Consider this query:
SELECT table1.id,
table1.review,
table1.time,
table2.author,
table2.title
FROM
table1, table2
WHERE table1.id = table2.id
AND table1.reviewer = '{$username}'
ORDER BY table1.id
Run Code Online (Sandbox Code Playgroud)
I'm using the above quite a lot around my site's code. I find that adding the table prefixes etc. before the column names can make the query very long and take up quite a lot of lines.
有没有办法使上述查询更简单/更容易?
首先,您可能希望为表提供更短的别名.此外,您使用的是隐式连接语法,这会使WHERE子句复杂化,因此一般不推荐使用.您可能希望使用更现代的显式语法:
SELECT t1.id, t1.review, t1.time, t2.author, t2.title
FROM table1 AS t1
JOIN table2 AS t2 ON (t2.id = t1.id)
WHERE t1.reviewer = '{$username}'
ORDER BY t1.id
Run Code Online (Sandbox Code Playgroud)
请注意,它JOIN是同义词INNER JOIN,AS关键字在定义表别名时是可选的.你可以简单地使用 ... FROM table1 t1 ...而不是... FROM table1 AS t1 ....