简体   繁体   English

如何找到2个表之间的mysql差异

[英]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. 图片和Seen_pictures,向用户显示“图片”表中的图片,然后将该图片(其在表中的ID)移动到Seen_Pictures,并向用户显示“图片”表中的新图片。 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. 我需要一个mysql方案,该方案将输出Pictures和Seen_pictures表之间的差异,这样我就知道用户未看到哪些图片,并可以输出它们。

I have this so far, but it only works for 1 user, I need it to account for many different users: 到目前为止,我有这个功能,但它仅适用于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"
);

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. 假定pic中包含所有者的用户ID。 It also returns the userid with the picture not seen. 它还返回未显示图片的用户ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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