[英]Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id
I'm building a form for an award where the nominees can choose multiple award categories to apply to, and answer a series of questions for each award they have chosen. 我正在建立一个奖项表,提名人可以选择多个奖项类别来申请,并针对他们选择的每个奖项回答一系列问题。
I have 2 tables. 我有2张桌子。
The first table
contains the award categories of the award: 第
first table
包含奖项的奖项类别:
id: 1, 2, 3, ...
id: 1、2、3,...
name: award1, award2, award3, ....
名称: award1,award2,award3,....
The second table
contains the answers with the nominee id, question id, and award category: second table
包含带有被提名人ID,问题ID和奖励类别的答案:
id:
ID:
answer_id: 1, 2, 3, ...
answer_id: 1、2、3,...
nominee_id: 1, 1, 2, ...
nominee_id:1,1,2 ,...
question_id: 1,2, 1, ...
问题ID: 1,2,1,...
category_id: 1, 3, 1, ...
category_id: 1、3、1,...
answer: answer 1, answer 2, answer 3, ...
答案:答案1,答案2,答案3,...
What I need is a query that can display the award categories that the nominee has already submitted answers for, as well as a second query that can display the award categories that are still available for the nominee to apply to. 我需要的是一个查询,该查询可以显示被提名人已经提交了答案的奖励类别,以及第二个查询,可以显示被提名人仍然可以申请的奖励类别。
So using the example above, I would like to see: 因此,使用上面的示例,我希望看到:
EDIT: 编辑:
Here are the tables, first one is the award categories, second one is the answers 这是表格,第一个是奖励类别,第二个是答案
As you can see, nominee with ID of 28 has answered a total of 16 questions, 8 per award category with id of 5 and 6. 如您所见,ID为28的被提名人共回答了16个问题,每个奖项类别有8个问题,ID为5和6。
The outputs I want are: 我想要的输出是:
a) display the names of the categories nominee 28 has entered in. In this case, Brand Engagement (id:5) and Corporate Social Enterprise (id:6) a)显示被提名人28进入的类别的名称。在这种情况下,品牌参与度(id:5)和企业社会企业(id:6)
b) render a dropdown menu consisting of only the categories nominee 28 has yet to enter. b)呈现一个仅包含被提名人28尚未进入的类别的下拉菜单。 In this case, award category id of 7-8.
在这种情况下,奖励类别ID为7-8。
Try Queries as below 尝试如下查询
Query-1 查询1
nominee_id 1 has already applied to award1 and award 3 nominee_id 1已应用于award1和award 3
select table1.*,table2.nominee_id,table2.answer_id,table2.question_id,
table2.category_id,table2.answer from table1
inner join table2 on table1.id = table2.category_id
Query-2 查询2
nominee_id 1 can still apply for award 2, award 4, ... nominee_id 1仍然可以申请裁决2,裁决4,...
select table1.*,table2.nominee_id,table2.answer_id,table2.question_id,
table2.category_id,table2.answer from table1
inner join table2 on table1.id <> table2.category_id
I have assumed fields, please ignore fields not required. 我假设使用字段,请忽略不需要的字段。
Building on Sameer's answer i would suggest: 基于Sameer的答案,我建议:
First Query (Categories with participation; effectively the same as Sameer's) 第一个查询(具有参与性的类别;与Sameer的类别有效)
SELECT table1.*, table2.nominee_id, table2.answer_id, table2.question_id, table2.category_id, table2.answer
FROM table1
JOIN table2 ON table2.category_id = table1.id
Second Query (Categories with no participation; less "noise") 第二个查询(类别,无参与;“噪音”少)
SELECT table1.*
FROM table1
LEFT JOIN table2 ON table2.category_id = table1.id
WHERE table2.category_id IS NULL
The second query uses the way a LEFT JOIN
works: if no matching row is found in table2 all columns are set to NULL
inside the result. 第二个查询使用
LEFT JOIN
工作方式:如果在table2中找不到匹配的行,则结果内的所有列都将设置为NULL
。 As the join criteria says table2.category_id = table1.id
table2.category_id
can only be NULL
if table1.id
is NULL
or there is no row. 作为连接标准说
table2.category_id = table1.id
table2.category_id
只能是NULL
如果table1.id
为NULL
或不存在的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.