这个SQl被认为是一个加入?

时间:2013-03-17 01:06:37

标签: mysql

我使用MySQl根据“模块”表中的类别ID从1个表中分类名称。

我有以下SQL可以满足我的需求,但我想知道这是否被视为JOIN

因为它没有调用JOIN

SELECT  `mo_category_fk` ,  `mo_name_vc` ,  `mc_name_vc`
FROM x_modcats mc, x_modules m
WHERE mc.mc_id_pk = m.mo_category_fk
AND m.mo_folder_vc =  :module

2 个答案:

答案 0 :(得分:3)

是 - 在MySQL中,隐式和显式联接具有相同的执行计划。您可以使用EXPLAIN验证这一点。但这是另一个线程的样本:

mysql> explain select * from table1 a inner join table2 b on a.pid = b.pid;
+----+-------------+-------+------+---------------+------+---------+--------------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref          | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+--------------+------+-------+
|  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL | NULL    | NULL         |  986 |       |
|  1 | SIMPLE      | a     | ref  | pid           | pid  | 4       | schema.b.pid |   70 |       |
+----+-------------+-------+------+---------------+------+---------+--------------+------+-------+
2 rows in set (0.02 sec)

mysql> explain select * from table1 a, table2 b where a.pid = b.pid;
+----+-------------+-------+------+---------------+------+---------+--------------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref          | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+--------------+------+-------+
|  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL | NULL    | NULL         |  986 |       |
|  1 | SIMPLE      | a     | ref  | pid           | pid  | 4       | schema.b.pid |   70 |       |
+----+-------------+-------+------+---------------+------+---------+--------------+------+-------+
2 rows in set (0.00 sec)

答案 1 :(得分:2)

是的,你正在加入。根据{{​​3}},,可以替代JOIN关键字..除非您不能使用非常有用的ON子句。但是,您具有连接WHERE子句中的表的条件。在我看来,将它作为FROM子句的一部分更有意义:

SELECT mo_category_fk, mo_name_vc, mc_name_vc
FROM x_modcats mc 
JOIN x_modules m ON (mc.mc_id_pk = m.mo_category_fk)
WHERE m.mo_folder_vc = :module