Tuesday, January 10, 2006

SQL to return rows in table1 BUT NOT table2

SQL code for saying "display records in table1 BUT NOT in table2", you have to think a little differently to make it work:


SELECT id,name,platform_id FROM platform LEFT JOIN platformblock ON id = platform_id AND site_id=19 WHERE platform_id IS NULL;


Think about how left joins work. It's like looping thru all the rows of the first table, and for every one of those, pulling in extra columns of data from the second table. If there is no match between the first and second tables on any data row, then the extra columns will be blank for that data row. So, you can demand that one of the columns be "NULL", as with the WHERE portion of the code above! That guarantees you only get back rows that are from table1 but not table2.



technorati tags: , , , ,

0 Comments:

Post a Comment

<< Home