Search code examples
phpmysqlduplicatessubquery

In mysql , looking to only show unique items from 2 queries of 2 tables with different column names


In mysql , looking to only show unique items from 2 queries of 2 tables with different column names. I can run them fine independently, but I want to create a list I can use in a dropdown list in PHP. I currently pull a list for Query 1 and it works fine. But I need to eliminate the choices found in Query 2. Hope that makes sense, this is not my career but only a side project !

Query 1 -

SELECT `driverName` 
FROM `A Drivers` 
WHERE `driverYear` = 2020

Query 1

Query 2 -

SELECT `driverA` 
FROM `user_picks` 
WHERE `userID` = 1 
and `raceYear` = 2020

Query 2

All of Query 2 will exist in Query 1. I don't want to show them in my result.

Thanks, Steve


Solution

  • You can use NOT IN

    SELECT `driverName` 
    FROM `A Drivers` 
    WHERE `driverYear` = 2020 
       AND `driverName` NOT IN (SELECT `driverA` 
                                FROM `user_picks` 
                                WHERE `userID` = 1 
                                AND `raceYear` = 2020)