问题描述
在两个不同的表之间使用逗号或联接有什么区别.
what is the difference between using comma or join between two different tables.
例如这两个代码:
SELECT studentId, tutorId FROM student, tutor;
SELECT studentId, tutorId FROM student JOIN tutor;
推荐答案
执行它们时并没有真正的区别,但是在工作中存在可读性,一致性和减轻错误的问题:
There's no real difference WHEN executing them, but there is a readability, consistency and error mitigating issue at work:
假设您有4张桌子如果您使用老式的方式进行INNER JOIN,则最终会得到:
Imagine you had 4 tablesIf you used the old fashioned way of doing an INNER JOIN, you would end up with:
SELECT col1, col2
FROM tab1, tab2, tab3,tab4
WHERE tab1.id=tab2.tab1_id
AND tab4.id = tab2.tab3_id
AND tab4.id = tab3.tab4_id;
使用显式的INNER JOINS将会是:
Using explicit INNER JOINS it would be:
SELECT col1, col2
FROM tab1
INNER JOIN tab2 ON tab1.id = tab2.tab1_id
INNER JOIN tab3 ON tab3.id = tab2.tab3_id
INNER JOIN tab4 ON tab4.id = tab3.tab4_id;
后者会显示您在表格前面的确切位置,这是JOINing的作用.它具有更高的可读性,并且更容易出错,因为与在WHERE中添加另一个AND或完全添加错误的条件(就像我在上面的查询中所做的一样)相比,忘记ON子句更容易忘记了.
The latter shows you right in front of the table exactly what is it JOINing with. It has improved readability, and much less error prone, since it's harder to forget to put the ON clause, than to add another AND in WHERE or adding a wrong condition altogether (like i did in the query above :).
此外,如果您正在执行其他类型的JOINS,则使用显式的编写方式,则只需将INNER更改为其他内容,代码便会得到一致的构建.
Additionally, if you are doing other types of JOINS, using the explicit way of writing them, you just need to change the INNER to something else, and the code is consistently constructed.
这篇关于逗号和sql之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!