[英]How to find mysql difference between 2 tables
圖片和Seen_pictures,向用戶顯示“圖片”表中的圖片,然后將該圖片(其在表中的ID)移動到Seen_Pictures,並向用戶顯示“圖片”表中的新圖片。 我需要一個mysql方案,該方案將輸出Pictures和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
假定pic中包含所有者的用戶ID。 它還返回未顯示圖片的用戶ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.