简体   繁体   中英

query in mysql to perform the not in function

I have written a query in mysql to filter out the vendorids' which are not in vendorids of the userid = 1 but they are present in vendorids of the userid = 5 but I am not getting the correct answer the query is below

select vendorid  
from rating_table 
where (userid = 5) not in (select vendorid from rating_table where userid = 1)  
order by vendorid asc;

thank you

You need to check if vendorid is not in the subquery result

SELECT vendorid 
FROM rating_table 
WHERE (userid = 5)
AND vendorid NOT IN 
(
    SELECT 
        vendorid 
    FROM rating_table 
    WHERE userid = 1
) 
ORDER BY vendorid ASC;

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