Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USING keyword in Mysql

Tags:

mysql

I have a table A with the following definition in MySQL

----------------------------------
id    c_id     t_id
-------------------------------

where c_id references cid on another table B with the following definition

================================================
id cid cname
=================================================

So i am issuing the following query

select group_concat(cname) as list
from A join B using (cid)
where t_id="something";

But I m getting the following error

Unknown column "cid" in from clause

I tried changing it to "c_id", but that doesnt seem to work either..

Any help is appreciated.

Thanks

like image 456
Shenoy Tinny Avatar asked Jul 18 '12 20:07

Shenoy Tinny


2 Answers

USING in MySQL is just a short form for a standard ON clause, and only works when the column name is identical in both tables.

From the manual:

The USING(column_list) clause names a list of columns that must exist in both tables.

Instead, do this:

select group_concat(B.cname) as list 
from A 
inner join B on A.c_id = B.cid 
where A.t_id = 'something';
like image 124
D'Arcy Rittich Avatar answered Nov 15 '22 17:11

D'Arcy Rittich


Both columns need the same name: either c_id or cid Then the using clause will work

like image 32
Steven Carnegie Avatar answered Nov 15 '22 17:11

Steven Carnegie