Find out the data that do not exist in the two tables.

assume that the current user_id = 1

uc table

prod_id / user_id
102 / 1
103 / 1

cs table

prod_id / user_id
102 / 1

I want to find out 103, that is, those data that are available in the uc table but not in the cs table

SELECT
    uc.prod_id
    FROM `user_cart` as uc 
    JOIN `user_cart_stock` as cs ON cs.prod_id = uc.prod_id
    WHERE uc.user_id = 1
    AND cs.prod_id IS NULL

I find that writing like this doesn"t print anything?

Sep.25,2021

you can change not in to not exists while using subqueries to optimize the query

select uc.prod_id from `uc`
where not exists (select `prod_id` from cs where prod_id=uc.prod_id);

it is recommended to take a look at these four join- > left join,right join,inner join,out join.
there is no data because of your last condition

AND cs.prod_id IS NULL

this sentence leads to


look at the conditions you write are uc.user_id = 1 and cs.prod_id IS NULL , and they are inline queries, which cannot be queried.

:

SELECT uc.prod_id FROM `uc` as uc  
LEFT JOIN `cs` as cs 
ON cs.prod_id = uc.prod_id 
WHERE uc.user_id = 1 
AND cs.prod_id IS NULL


:

select uc.prod_id from `uc`
where `prod_id` not in (select `prod_id` from cs);

Note that the table name is different from yours.

Menu