-2

I know the forum is full of this questions but i cant find the solution. I want to join the table users from user_to_designment they both have a column with user_id the error that i get is:

select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON  user_to_designment.user_id = users.user_id


 LIMIT 0, 25 
MySQL meldt: Documentatie

#1052 - Column 'user_id' in field list is ambiguous 

I use this query:

select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON  user_to_designment.user_id = users.user_id

Please some advice

5 Answers5

1

In your select list prefix the user_id with the table name:

select users.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON  user_to_designment.user_id = users.user_id

Both columns have user_id, SQL cannot choose between them, you must specify explicitly.

pritaeas
  • 2,073
  • 5
  • 34
  • 51
0

You need to specify which user_id to return in your select, eg select users.user_id or Select user_to_designment.user_id

OTTA
  • 1,071
  • 7
  • 8
0

You must clarify which user_id table column you are selecting.

select a.user_id,b.designment_id from user_to_designment b
full join users a
on b.user_id=a.user_id
kostas
  • 461
  • 5
  • 13
0

This is because "user_id" field exists on both tables. you must put table name before field name.

    select user_to_designment.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON  user_to_designment.user_id = users.user_id
0

It seems user_id exist in both the tables. so it gives the ambiguous error. We should say from which table we have to pick the column.

SELECT users.user_id
    ,user_to_designment.designment_id
FROM user_to_designment
FULL JOIN users ON user_to_designment.user_id = users.user_id

You may use either users.user_id or user_to_designment.user_id in your select statement. Always use tablename.columnname format. It avoids confusion.

StackUser
  • 5,370
  • 2
  • 24
  • 44