简体   繁体   English

连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行

[英]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: 因此,使用上面的示例,我希望看到:

  1. nominee_id 1 has already applied to award1 and award 3 nominee_id 1已应用于award1和award 3
  2. nominee_id 1 can still apply for award 2, award 4, ... nominee_id 1仍然可以申请裁决2,裁决4,...

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.idNULL或不存在的行。

暂无
暂无

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

相关问题 PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? PHP MYSQL - 突出显示数据库表行IF table1.id存在于table2中 - PHP MYSQL - Highlight a database table row IF table1.id exists in table2 通过在Table1.id上应用groupBy,但在Table2上没有软删除的行,使用Laravel查询生成器来计算总和? - Calculate sum using Laravel Query Builder by applying groupBy on Table1.id but without soft deleted rows on Table2? PHP连接两个表,其中一个表中的两列包含from_ID和to_ID引用另一表中的id - PHP join two table where two column in one table contains from_ID and to_ID refer to id in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM