如何查找两个表之间的mysql差异


How to find mysql difference between 2 tables

Pictures和Seen_Pictures,将向用户显示"图片"表中的一张图片,然后将该图片(该图片在表中的ID)移动到Seen_Pictures,并向用户显示图片表中的新图片。我需要一个mysql方案,它将输出图片和Seen_Pictures表之间的差异,这样我就知道用户没有看到什么图片,并可以输出它们。

到目前为止,我有这个,但它只适用于1个用户,我需要它来解释许多不同的用户:

$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"
);

怎么样

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

我认为您可以对原始查询进行一些小的修改,以获得您想要的:

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   

这假设图片中有一个所有者的用户id。它还返回了没有看到图片的用户id。