SQL - WHERE与两个表和JOIN之间的区别

时间:2013-10-13 12:52:54

标签: mysql sql

以下是这两个SQL查询:

SELECT `o`.`date`, `i`.`description`, `o`.`quantity`
FROM `orders` AS `o`, `items` AS `i`
WHERE `i`.`itemID` = `o`.`itemID`;

SELECT `o`.`date`, `i`.`description`, `o`.`quantity`
FROM `orders` AS `o`
JOIN `items` AS `i`
ON `i`.`itemID` = `o`.`itemID`;

第一个结果减少了一个结果。 有人可以解释两者之间的区别吗?

2 个答案:

答案 0 :(得分:0)

这是你想要做的usefull discussion

SQL left join vs multiple tables on FROM line?

希望它有所帮助!

答案 1 :(得分:0)

它们在功能上是等效的,因为MySQL优化器将theta连接重写为ANSI JOIN(至少MySQL 5.6)

您可以使用EXPLAIN EXTENDED your_query进行检查;显示警告;

theta style join

SELECT `o`.`date`, `i`.`description`, `o`.`quantity`
FROM `orders` AS `o`, `items` AS `i`
WHERE `i`.`itemID` = `o`.`itemID`;

ANSI JOIN

SELECT `o`.`date`, `i`.`description`, `o`.`quantity`
FROM `orders` AS `o`
JOIN `items` AS `i`
ON `i`.`itemID` = `o`.`itemID`;

唯一真正的区别在于可读性theta风格让它更容易被误认为它是一个INNER JOIN,所以养一个习惯就可以编写ANSI JOIN了