Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown column error in full join syntax

Tags:

sql

join

mysql

A friend and I were playing around with the JOIN clauses available in MySQL. Here is the table structure we have:

create table abc 
(
  c1 int,
  c2 char(1)
);
create table xyz
(
  c2 char(1),
  c3 varchar(5)
);

The values inserted into them are:

insert into abc
values
  (1,"a"),
  (2,"b");
insert into xyz
values
  ("a","apple"),
  ("c","car");

Now, I have the following two SQL statements (sqlfiddle link):

select
  *,
  'full'
from abc
full join xyz
  using (c2);

and

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

As can be seen in the fiddle, the first query returns:

| C2 | C1 |    C3 | FULL |
|----|----|-------|------|
|  a |  1 | apple | full |

while, the second query raises and error:

Unknown column 'abc.c2' in 'on clause':

Why is the column abc.c2 not recognised when using the ON clause of JOIN syntax?

like image 360
hjpotter92 Avatar asked Sep 06 '14 16:09

hjpotter92


People also ask

What is the syntax for full join in in?

SELECT columns FROM table1 name FULL JOIN table2 name ON table1. coumn_x = table2.

Why full join is not working in MySQL?

MySQL does not support FULL JOIN, so you have to combine JOIN, UNION and LEFT JOIN to get an equivalent. It gives the results of A union B. It returns all records from both tables. Those columns which exist in only one table will contain NULL in the opposite table.


1 Answers

FULL JOIN is not supported by MySQL. It's because full is recognized as alias to abc table. Try this query, it runs without errors:

select
  *,
  'full'
from abc
full join xyz
  on full.c2 = xyz.c2;
like image 159
Rimas Avatar answered Oct 15 '22 21:10

Rimas