何时使用join或简单2表条件? [重复]

时间:2021-11-10 12:58:27

Possible Duplicate:
SQL left join vs multiple tables on FROM line?

可能重复:SQL左连接与FROM行上的多个表?

I think this is much clearer:

我认为这更清楚:

"SELECT * FROM t1,t2 WHERE t2.foreignID = t1.id "

than a query with a JOIN.

而不是具有JOIN的查询。

are there any specs on when to use one or another?

什么时候使用这个或那个有什么规格?

Thanks

2 个解决方案

#1


2  

Personally, I prefer the explicitness of

就个人而言,我更喜欢显性

SELECT t1.*, t2.*
FROM t1 
JOIN t2 ON t1.id = t2.foreignID

I like to see exactly what my JOIN conditions are, and the I use WHERE to further filter the results (current year, only a certain user, etc). It shouldn't matter in a simple query like this, but will definitely help with longer, more complex queries.

我想确切地看到我的JOIN条件是什么,并且我使用WHERE来进一步过滤结果(当前年份,仅限某个用户等)。在这样的简单查询中它应该无关紧要,但肯定会有助于更长,更复杂的查询。

It doesn't make sense to me to have one style for shorter queries and a different for longer ones. Usually the simple ones turn into complex queries soon enough.

对于我来说,对于较短的查询使用一种样式而对于较长的查询则使用不同的样式是没有意义的。通常,简单的问题很快就会变成复杂的查询。

#2


0  

This is a cartesian join which is actually very, very slightly different from an inner join. More than 99.99% of the time, you will get identical results. But there are edge cases that will run much slower, or possibly give extra rows. But you are more likely to run into parsing problems when you add left joins. The MySQL documentation gives a little bit of explanation, but not much. The differences lie in how the query optimiser chooses how to scan the tables.

这是一个笛卡尔连接,实际上与内连接非常非常不同。超过99.99%的时间,您将获得相同的结果。但是边缘情况会慢得多,或者可能会产生额外的行。但是,当您添加左连接时,您更有可能遇到解析问题。 MySQL文档给出了一些解释,但并不多。不同之处在于查询优化器如何选择如何扫描表。

This format also gets messy when you start adding other conditions in your WHERE clause.

当您开始在WHERE子句中添加其他条件时,此格式也会变得混乱。

#1


2  

Personally, I prefer the explicitness of

就个人而言,我更喜欢显性

SELECT t1.*, t2.*
FROM t1 
JOIN t2 ON t1.id = t2.foreignID

I like to see exactly what my JOIN conditions are, and the I use WHERE to further filter the results (current year, only a certain user, etc). It shouldn't matter in a simple query like this, but will definitely help with longer, more complex queries.

我想确切地看到我的JOIN条件是什么,并且我使用WHERE来进一步过滤结果(当前年份,仅限某个用户等)。在这样的简单查询中它应该无关紧要,但肯定会有助于更长,更复杂的查询。

It doesn't make sense to me to have one style for shorter queries and a different for longer ones. Usually the simple ones turn into complex queries soon enough.

对于我来说,对于较短的查询使用一种样式而对于较长的查询则使用不同的样式是没有意义的。通常,简单的问题很快就会变成复杂的查询。

#2


0  

This is a cartesian join which is actually very, very slightly different from an inner join. More than 99.99% of the time, you will get identical results. But there are edge cases that will run much slower, or possibly give extra rows. But you are more likely to run into parsing problems when you add left joins. The MySQL documentation gives a little bit of explanation, but not much. The differences lie in how the query optimiser chooses how to scan the tables.

这是一个笛卡尔连接,实际上与内连接非常非常不同。超过99.99%的时间,您将获得相同的结果。但是边缘情况会慢得多,或者可能会产生额外的行。但是,当您添加左连接时,您更有可能遇到解析问题。 MySQL文档给出了一些解释,但并不多。不同之处在于查询优化器如何选择如何扫描表。

This format also gets messy when you start adding other conditions in your WHERE clause.

当您开始在WHERE子句中添加其他条件时,此格式也会变得混乱。