mysql|来自两个表的条件查询


mysql | Conditional query from two tables

我有两个表:

问题

id title
1  First question
2  Second question
3  Third question

答案

id   text    question_id
1    Answer1 1
2    Answer2 1
3    Answer3 2

我正在搜索返回未回答问题的查询(上例中的问题id:3)?谢谢

Select id, title from question A Left Join answer B on A.id=B.question_id where B.question_id  is null

由于Mysql不支持MINUS(或者至少我不知道),您必须使用join

SELECT q.id
FROM questions q LEFT JOIN answers a
ON q.id = a.question_id
WHERE a.question_id is null
select id,title from questions where id NOT IN(select distinct(question_id) from answers)

mysql中的NOT IN将帮助您

试试这个:

select * from questions left outer join answers on questions.id = question_id
where question_id is null

select q.id,q.title from questions q where q.id NOT IN(select distinct(question_id) from answers)

未使用是安全查询以查找已回答的问题。