简体   繁体   English

MySQL Join; 仅查询不匹配的表字段

[英]MySQL Join; Query only non-matching table fields

Hopefully, I can explain this clearly, but, for about two days now, I have been trying to select data from one table that does not exist in the other table. 希望我可以清楚地解释这一点,但是大约两天来,我一直在尝试从一个表中选择另一个表中不存在的数据。

So, basically, I want to select from a list of tracks that have not already been listened to . 因此,基本上,我想从尚未收听的曲目列表中进行选择。

SELECT *
FROM tracks JOIN listens
ON listens.user_id = tracks.user_id
WHERE listens.user_id != 9;

PS: I have tried many solutions including unions and subqueries. PS:我尝试了很多解决方案,包括联合和子查询。 This is where I got to before giving up on figuring it out myself. 这是我在放弃自己弄清楚之前必须去的地方。

My table setup: 我的表设置:

[tracks] ID|user_id [曲目] ID | user_id

[LISTENS] ID|track_id|user_id [LISTENS] ID | track_id | user_id

I want only the tracks that have not been listened to, and, for some reason I cannot achieve this. 我只需要未曾听过的曲目,由于某种原因,我无法做到这一点。 It just returns all of the tracks, or even sometimes discludes tracks not yet listened to. 它只是返回所有曲目,甚至有时会排除尚未收听的曲目。

Any points on the correct approach ? 关于正确方法有什么要点吗? Would be greatly appreciated! 将不胜感激! Thanks much :D 非常感谢:D

To find tracks that user 9 has not listed to, here is one method: 要查找用户9尚未列出的曲目,这是一种方法:

SELECT t.*
FROM tracks t left join
     listens l
     on l.track_id = t.id AND l.user_id = 9
WHERE l.user_id is null;

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

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