patternsqlMinor
sql producing duplicate column id on join?
Viewed 0 times
producingcolumnsqlduplicatejoin
Problem
Is there any way to only show one id column which is used to join on other tables?
This query produces two id columns used to join each other as primary and foreign key. I only want it to repeat the id column once.
Query results:
This query produces two id columns used to join each other as primary and foreign key. I only want it to repeat the id column once.
SELECT *
FROM products
JOIN productimg ON products.id = productimg.id
WHERE products.id IN (1, 2, 3)Query results:
id name cid stk prc id url
1 product1 1 10 3000 1 /static/images/dresses/td1.jpg
2 product2 2 10 2500 2 /static/images/dresses/td2.jpg
3 product3 2 4 4000 3 /static/images/dresses/td3.jpgSolution
Is there any way to only show one id column which is used to join on other tables?
USING collapses two columns into one which is placed first. If more than one column is used for joining they are listed according to the position in USING clause text. So the output columns order is: ``.
If LEFT JOIN is used then the values are taken from left table.
You may to refer to a column in definite table specifying the table alias (really - needed only in the case of LEFT JOIN for to refer to right table).
The same is applicable to NATURAL JOIN (it is an equivalent of USING when all common columns are listed in joining expression).
Demo fiddle
SELECT *
FROM products
JOIN productimg USING (id)
WHERE products.id IN (1, 2, 3)USING collapses two columns into one which is placed first. If more than one column is used for joining they are listed according to the position in USING clause text. So the output columns order is: ``.
If LEFT JOIN is used then the values are taken from left table.
You may to refer to a column in definite table specifying the table alias (really - needed only in the case of LEFT JOIN for to refer to right table).
The same is applicable to NATURAL JOIN (it is an equivalent of USING when all common columns are listed in joining expression).
Demo fiddle
Code Snippets
SELECT *
FROM products
JOIN productimg USING (id)
WHERE products.id IN (1, 2, 3)Context
StackExchange Database Administrators Q#276065, answer score: 4
Revisions (0)
No revisions yet.