完整连接语法中的未知列错误

时间:2014-09-06 16:36:34

标签: mysql sql join

我和朋友正在玩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

1 个答案:

答案 0 :(得分:11)

MySQL不支持

FULL JOIN。这是因为full被识别为abc表的别名。试试这个查询,它运行没有错误:

select
  *,
  'full'
from abc
full join xyz
  on full.c2 = xyz.c2;