我和朋友正在玩MySQL中的JOIN
子句。这是我们的表结构:
create table abc
(
c1 int,
c2 char(1)
);
create table xyz
(
c2 char(1),
c3 varchar(5)
);
插入其中的值为:
insert into abc
values
(1,"a"),
(2,"b");
insert into xyz
values
("a","apple"),
("c","car");
现在,我有following two SQL statements(sqlfiddle link):
select
*,
'full'
from abc
full join xyz
using (c2);
和
select
*,
'full'
from abc
full join xyz
on abc.c2 = xyz.c2;
从小提琴中可以看出,第一个查询返回:
| C2 | C1 | C3 | FULL |
|----|----|-------|------|
| a | 1 | apple | full |
while,第二个查询引发错误:
Unknown column 'abc.c2' in 'on clause':
为什么在使用abc.c2
语法的ON
子句时无法识别列JOIN
?
答案 0 :(得分:11)
FULL JOIN
。这是因为full
被识别为abc
表的别名。试试这个查询,它运行没有错误:
select
*,
'full'
from abc
full join xyz
on full.c2 = xyz.c2;