简体   繁体   中英

How do I add an alias in a SQL inner join?

I have two tables like so:

Table Name: Foo
Foo Columns: (ID, UNIQUE_ID, NAME)

Table Name: Bar
Bar Columns (FOO_ID, UNIQUE_ID, NAME)

I basically want all bars that belong to a particular foo , and the result should be two columns UNIQUE_ID and NAME of each bar .

My SQL looks like so:

SELECT UNIQUE_ID, NAME FROM BAR B INNER JOIN FOO F ON F.ID = B.FOO_ID WHERE F.UNIQUE_ID = 123

I provide the UNIQUE_ID . The problem is that both tables have a UNIQUE_ID column, so I receive the following error: SQL Error: ambiguous column name: UNIQUE_ID . How do I add in alias for the column in Foo so that my result of UNIQUE_ID and NAME contains the unique ID of Bar ? I don't want an alias for the UNIQUE_ID column in Bar .. the result should have the actual column name.

Is this what you want?

SELECT B.UNIQUE_ID, B.NAME
FROM BAR B INNER JOIN
     FOO F
     ON F.ID = B.FOO_ID
WHERE F.UNIQUE_ID = 123

You should qualify all column references in the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM