简体   繁体   English

如何连接表,包括左表中的所有id,但只显示某些where子句的右表中的信息

[英]How to join tables including all ids from left table but only showing information from the right table given certain where clause

I have an attendees table with the following structure: 我有一个具有以下结构的与会者表:

+--------------+---------+
| attendee_id  | others1 |
+--------------+---------+
|    abcd      | A       |
|    ghij      | B       |
|    defg      | C       |
+--------------+---------+

And also an eventattendees table with the following structure: 还有一个具有以下结构的eventattendees表:

+--------------+---------+----------+
| attendee_id  | others2 | event_id |
+--------------+---------+----------+
|    wxyz      | D       |     1    |
|    mlno      | E       |     2    |
|    defg      | F       |     3    |
|    defg      | G       |     2    |
|    abcd      | H       |     1    |
+--------------+---------+----------+

What I want is to create a query that, given some event_id, returns a join of these tables (by attendee_id) that includes all attendee ids from attendee table and also returns the information from the eventattendde tables which a match for that event_id . 我想要的是创建一个查询,给定一些event_id,返回这些表的连接(通过attendee_id),其中包括来自与会者表的所有与会者ID,并返回eventattendde表中与该event_id匹配的信息。 Say, for event_id 3: 说,对于event_id 3:

+--------------+---------+---------+----------+
| attendee_id  | others1 | others2 | event_id |
+--------------+---------+--------------------+
|    abcd      | A       |  null   |   null   |
|    ghij      | B       |  null   |   null   |
|    defg      | C       |    F    |     3    |
+--------------+---------+--------------------+

How can I do that for mysql? 我怎么能为mysql做到这一点?

You need to put your where criteria in the join instead to respect the outer join . 您需要将您的where标准放在join而不是尊重outer join

select a.attendee_id, a.others1, e.others2, e.event_id
from attendees a
   left join eventattendees e on a.attendee_id = e.attendee_id and e.event_id = 3

If you put it in the where criteria, it will negate the outer join . 如果将其放在where条件中,它将取消outer join

Use left join 使用左连接

select a.attendee_id, a.others1, b.others2, b.event_id 
from attendees  as a 
left join eventattendees on a.attendee_id = b.attendee_id and b.event_id= 3;

你可以试试这个:

SELECT A.attendee_id, A.others1, ISNULL(B.others2,'empty') AS others2, ISNULL(B.event_id,'empty') AS event_id FROM TableA A LEFT JOIN TableB B ON A.attendee_id=B.attendee_id

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

相关问题 当我只需要WHERE子句中的联接(右)表中的一列时,是否需要左外部联接? - Is a left outer join needed when I only need one column from the joined (right) table in the WHERE clause? MySQL Join - 仅当所有右表行都满足 WHERE 子句时才检索左表行 - MySQL Join - Retrieve Left Table Rows only if all the Right Table Rows satisfies the WHERE clause Laravel 2表联接,左侧表的所有表与具有给定FK值的右侧表的成员合并 - Laravel 2 table join with all from left table merged with members of the right table with a given FK value 左连接 - Select ALL 来自左表,但 select 仅来自右表的最新 - Left Join - Select ALL from left table but select only the latest from right table 如何使WHERE子句仅适用于左连接中的右表? - How can I make a WHERE clause only apply to the right table in a left join? 一个奇怪的Oracle SQL,带有3个表,但只在where子句中保留了2个联接? - A strange Oracle SQL with from 3 tables but only left join 2 in where clause? 从where子句的给定集中选择表中不存在的ID - Select IDs that do not exist in a table from a given set in where clause 带有where子句的LEFT JOIN仅用于第二个表 - LEFT JOIN with where clause only for second table 左联接中仅一个表的where子句 - Where clause for only one table in a left join SQL 左连接不包括左表中的所有记录 - SQL left join is not including all records from left table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM