I have two listboxes,listbox1 and listbox2, and two tables in a DB, table1 and table2. Both tables contain a column called 'Colour'. I want to display all 'Colours' from table1 in listbox1 which I'm able to do. But in listbox2 I want to display 'Colours' from Table2 but it must not be present in the Table1 'Colours'. So how do I write a query for it?
This is what I have been trying and its not working:
SELECT Table2.Colour
FROM Table1 CROSS JOIN Table2
WHERE (Tabel1.Colour! = Table2.colour)
Error Message is — multi-part identifier Tabel2.Colour could not be found
This should work with a LEFT JOIN
:
SELECT Table2.Colour
FROM Table2
LEFT JOIN Table1 ON Tabel1.Colour = Table2.colour
WHERE Table1.Colour IS NULL
You could also use NOT IN
or NOT EXISTS
, I just prefer the LEFT JOIN
/ IS NULL
syntax.
For example:
SELECT Colour
FROM Table2
WHERE Colour NOT IN (SELECT Colour FROM Table1)
If your version of RDBMS implements EXCEPT
clause, you can do the following:
SELECT Colour FROM Table2
EXCEPT
SELECT Colour FROM Table1
I'm not saying that this is better than JOIN. Just another option.
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.