sandy1028 Posted March 3, 2008 Share Posted March 3, 2008 I have to 3 tables Table1,table2,table3 and in all tables field name `fieldname` is similar . I have to select values from table1 the values which is not in table2 and table3. Select distinct(fieldname) from table1 where fieldname not in (select distinct(fieldname) from table2 union (select distinct(fieldname) from table3) But I am gtting the proper result Link to comment https://forums.phpfreaks.com/topic/94132-select-statement-from-two-tables/ Share on other sites More sharing options...
lenerd3000 Posted March 4, 2008 Share Posted March 4, 2008 im not sure about this but i think it worth a try... Select distinct(fieldname) from table1 where fieldname not exists (select distinct(fieldname) from table2 and not exists (select distinct(fieldname) from table3) Link to comment https://forums.phpfreaks.com/topic/94132-select-statement-from-two-tables/#findComment-483153 Share on other sites More sharing options...
fenway Posted March 4, 2008 Share Posted March 4, 2008 A LEFT JOIN on the union'ed table could work. Link to comment https://forums.phpfreaks.com/topic/94132-select-statement-from-two-tables/#findComment-483273 Share on other sites More sharing options...
aschk Posted March 5, 2008 Share Posted March 5, 2008 I'm curious to know the database layout as it would seem that the tables aren't normalised at first glance. SELECT t1.fieldname FROM table1 t1 LEFT JOIN table2 t2 ON t1.fieldname = t2.fieldname LEFT JOIN table3 t3 ON t1.fieldname = t3.fieldname WHERE t2.fieldname IS NULL OR t3.fieldname IS NULL Link to comment https://forums.phpfreaks.com/topic/94132-select-statement-from-two-tables/#findComment-483741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.