SQL column reference "id" is ambiguous
Asked Answered
D

6

101

I tried the following select:

SELECT (id,name) FROM v_groups vg 
INNER JOIN people2v_groups p2vg ON vg.id = p2vg.v_group_id
WHERE p2vg.people_id =0;

And, I get the following error column reference id is ambiguous.

If I try the same SELECT, but I only ask for name and not for id also, it works.

Any suggestions?

Demonstrate answered 22/3, 2012 at 11:9 Comment(2)
Once you have more than one table in your statement you should alway prefix your column names with the corresponding table alias. That way you'll never get this error.Heteropterous
This issue actually happens when there is same column name in both tables. where <tableName.columnName> = <value> can solve this issue.Dandridge
T
136

You need the table name/alias in the SELECT part (maybe (vg.id, name)) :

SELECT (vg.id, name)
FROM v_groups vg 
INNER JOIN people2v_groups p2vg
  ON vg.id = p2vg.v_group_id
WHERE p2vg.people_id = 0;
Trickster answered 22/3, 2012 at 11:11 Comment(0)
N
11

I suppose your p2vg table has also an id field , in that case , postgres cannot find if the id in the SELECT refers to vg or p2vg.

you should use SELECT(vg.id,vg.name) to remove ambiguity

Nombles answered 22/3, 2012 at 11:12 Comment(1)
Yeah as the matter of fact the id field is duplicate.Demonstrate
A
6
SELECT (vg.id, name) FROM v_groups vg 
INNER JOIN people2v_groups p2vg ON vg.id = p2vg.v_group_id
WHERE p2vg.people_id = 0;
Affirmation answered 22/3, 2012 at 11:15 Comment(0)
S
1
SELECT vg.id, 
       vg.name
  FROM v_groups vg INNER JOIN  
       people2v_groups p2vg ON vg.id = p2vg.v_group_id
 WHERE p2vg.people_id = 0;
Subordinate answered 25/9, 2017 at 6:55 Comment(0)
K
1

As a additional note: I got this error when I was using a CTE for a join resulting in an ambiguity. I was using the CTE in FROM with an alias and despite prefixing the SELECTed column with the CTE's alias, postgres would still produce this error on the prefixed column call. It was a bit trickier to discover as my query was long.

Hope it helps someone out there.

Kowtow answered 24/5, 2022 at 10:6 Comment(1)
what is the solution? I have the same problemSippet
A
0

I created person table, then inserted 2 rows into it as shown below:

CREATE TABLE person (
  id INTEGER,
  name VARCHAR(20)
);

INSERT INTO person (id, name) 
VALUES (1, 'John'), (2, 'David');

Then, I created my_func() which returns person table as shown below:

CREATE FUNCTION my_func() RETURNS TABLE(id INTEGER, name VARCHAR(20))
AS $$
BEGIN
  RETURN QUERY SELECT id, name FROM person;
END;        -- ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
$$ LANGUAGE plpgsql;

Finally, calling my_func() got the same error as shown below:

postgres=# SELECT my_func();
ERROR:  column reference "id" is ambiguous
LINE 1: SELECT id, name FROM person
               ^
DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
QUERY:  SELECT id, name FROM person
CONTEXT:  PL/pgSQL function my_func() line 3 at RETURN QUERY

So, I set the table name person with . just before id and name as shown below:

CREATE FUNCTION my_func() RETURNS TABLE(id INTEGER, name VARCHAR(20))
AS $$
BEGIN
  RETURN QUERY SELECT person.id, person.name FROM person;
END;               -- ↑ ↑ ↑ ↑    ↑ ↑ ↑ ↑
$$ LANGUAGE plpgsql;

Finally, I could call my_func() without error as shown below:

postgres=# SELECT my_func();
  my_func
-----------
 (1,John)
 (2,David)
(2 rows)

*Memos:

  • Omitting FROM clause from SELECT statement gets the error

  • If the number of the columns in RETURNS TABLE(...) doesn't match the number of the returned columns, there is the error.

Axenic answered 13/1 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.