简体   繁体   中英

How to find mysql difference between 2 tables

Pictures and Seen_pictures, a Picture from the Pictures table is displayed to a user, that Picture (it's ID in the table) is then moved to the Seen_Pictures, and a new picture from the Pictures table is shown to the user. I need a mysql scheme that will output the difference between the Pictures and Seen_pictures table, that way I know what pictures a user hasn't seen, and can output them.

I have this so far, but it only works for 1 user, I need it to account for many different users:

$result = mysqli_query(
    $link, 

    "SELECT o_Pics.Pic.PicID 
    FROM o_Pics.Pic 
    LEFT JOIN o_SeenPics.Seen ON o_Pics.Pic.PicID=o_SeenPics.Seen.PicID 
    WHERE NOT o_Pics.Pic.ID='".$ID."' AND o_SeenPics.Seen.PicID IS NULL"
);

How about

SELECT p.p_id FROM Picture p WHERE p.p_id NOT IN 
   (SELECT s.p_id FROM Seen_Picture s WHERE s.u_id = "$user_id")

Picture
p_id(Primary Key) picture

Seen_Picture
id(Primary Key) u_id p_id

I think you can make some minor modifications to your original query to get what you want:

SELECT s.UserId, p.PicID 
FROM o_Pics.Pic p LEFT JOIN
     o_SeenPics.Seen s
     ON p.PicID = s.PicID and
        p.OwnerUserId != s.UserId
where s.PicId is null and p.OwnerUserId != s.UserId   

This assumes that pic has a user id of the owner in it. It also returns the userid with the picture not seen.

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