简体   繁体   English

Mysql select 表 1 中的所有条目,其中表 2 中没有具有特定 ID 的条目

[英]Mysql select all entries from table 1 where there is no entries in table 2 with certain id

I have two tables, table surveys and table survey_votes.我有两个表,表调查和表 survey_votes。

I want to select all surveys from table surveys我想要 select 表格调查中的所有调查

select * from surveys s 

where there is no entry in table survey_votes equal to a certain user_id that references the survey from table surveys.其中表 survey_votes 中没有条目等于某个 user_id,它引用了表 surveys 中的调查。

Example:例子:

Table surveys表格调查

id ID question问题
1 1个 What food do you like best?你最喜欢什么食物?
2 2个 What is your favorite car brand?你最喜欢的汽车品牌是什么?

Table survey_votessurvey_votes

id ID user_id用户身份 survey_id调查编号
1 1个 100 100 1 1个
1 1个 101 101 1 1个
1 1个 101 101 2 2个
  • If user_id = 100, only survey 2 should be selected.如果 user_id = 100,则只应选择调查 2。 If user_id = 101, no survey should be selected.如果 user_id = 101,则不应选择任何调查。
  • If user_id = 102, both surveys should be selected.如果 user_id = 102,则应选择两个调查。

The output table should consist of all columns of the original table surveys. output 表应包含原始表调查的所有列。 Therefore, for user 102 it should look as follows:因此,对于用户 102,它应该如下所示:

id ID question问题
1 1个 What food do you like best?你最喜欢什么食物?
2 2个 What is your favorite car brand?你最喜欢的汽车品牌是什么?

Any help is much appreciated!任何帮助深表感谢!

SELECT * FROM `survey` s WHERE s.id  NOT IN  
(SELECT sv.`survey_id` FROM `survey_votes` sv WHERE sv.`user_id`=102)

This should help, change "102" with your input user_id这应该有所帮助,用您输入的 user_id 更改“102”

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

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