我一直认为,并且在Google / SO seems to confirm上进行搜索时,它们的作用相同:
SELECT ... FROM a
INNER JOIN b ON (a.foo = b.foo AND a.bar = b.bar)
应等效于
SELECT ... FROM a
INNER JOIN b USING (foo, bar)
即对于两个表具有相同列名的简单/一般情况,USING
只是语法糖。
现在,我在Debian 9和10上观察到MariaDB的不同行为,其中9达到了我的预期,而10却没有。
Debian 9:mysqld Ver 10.1.26-MariaDB-0+deb9u1 for debian-linux-gnu on x86_64 (Debian 9.1)
Debian 10:mysqld Ver 10.3.17-MariaDB-0+deb10u1 for debian-linux-gnu on x86_64 (Debian 10)
现在我有一个查询,该查询应该列出所有数据库中的所有约束及其相应的更新和删除规则。
这对两个都有效:
SELECT a.CONSTRAINT_NAME, b.UPDATE_RULE, b.DELETE_RULE
FROM information_schema.KEY_COLUMN_USAGE a
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS b ON (
a.CONSTRAINT_CATALOG = b.CONSTRAINT_CATALOG
AND a.CONSTRAINT_SCHEMA = b.CONSTRAINT_SCHEMA
AND a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
)
这将在Debian 10上返回一个空集,但可在9上运行:
SELECT a.CONSTRAINT_NAME, b.UPDATE_RULE, b.DELETE_RULE
FROM information_schema.KEY_COLUMN_USAGE a
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS b
USING (CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME)
EXPLAIN
在两个查询中看起来也相同:
+------+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------------------------------------------+
| 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | NULL | Open_full_table; Scanned all databases |
| 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | NULL | Using where; Open_full_table; Scanned all databases; Using join buffer (flat, BNL join) |
+------+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------------------------------------------+
这是怎么回事?