简体   繁体   中英

mysql how to select from one table if the second table has no matching information

I am trying to only show results from table inventoryfolders if the id does not exist in table newlisting.

Here is my query:

$sql = "SELECT * 
          FROM avirobust.inventoryfolders  
         WHERE parentFolderID = '".$foname."' 
           AND agentID = '".$mne."' 
           AND parentFolderID NOT IN (SELECT folderID FROM marketplace.newlisting)";

However it never returns a result even if newlisting has no data in it.

I know that join can work but can I use something like in my query above? If so what did I do wrong in my query?

You can try use this code

SELECT inventoryfolders.* FROM avirobust.inventoryfolders LEFT JOIN marketplace.newlisting on (inventoryfolders.parentFolderID = newlisting.folderID) WHERE parentFolderID = '".$foname."' AND agentID = '".$mne."' AND newlisting.folderID is NULL

It will return all records from inventoryfolders where no corresponding rows in newlisting

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM