简体   繁体   English

mysql加入条件行为

[英]mysql join on condition behavior

I am failing to understand the behavior of conditional join. 我无法理解条件连接的行为。 Here is a comment table where there are two types of users. 这是一个注释表,其中有两种类型的用户。 if the user_type_id=1 then the data should come from admins table, if the user_type_id=2 then the data should come from users table. 如果user_type_id = 1,则数据应来自admins表;如果user_type_id = 2,则数据应来自users表。

在此处输入图片说明

So I wrote the query as follows: 因此,我将查询编写如下:

 select a.name as admin_name, concat(u.first_name, ' ', u.last_name) as user_name, ptc.*
    from project_task_comments ptc 
    left join admins a on a.id= ptc.user_id and ptc.user_type_id=1
    left join users u on u.id=ptc.user_id and ptc.user_type_id=2
    and ptc.id=1

And Here is the result: 结果如下:

在此处输入图片说明

my question is why (in the result set) the row 3 and 5 has admin_name and user_name is null? 我的问题是为什么(在结果集中)第3和第5行的admin_name和user_name为null? Am I missing something? 我想念什么吗?

admins table: 管理员表:

+----+----------------+
| id | name           |
+----+----------------+
|  1 | ADMIN John Doe |
+----+----------------+

users table: 用户表:

+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | User       | Alice     |
+----+------------+-----------+

As per your data, there're three records for "ptc.user_type_id=2", 1. ptc.id = 1 2. ptc.id = 3 3. ptc.id = 5 In your query you've already mentioned to put records which belongs to the first instance (ie ptc.id = 1). 根据您的数据,有3条记录“ ptc.user_type_id = 2”,1。ptc.id = 1 2. ptc.id = 3 3. ptc.id = 5在您的查询中,您已经提到要放入属于第一个实例的记录(即ptc.id = 1)。 So if you wants all three then you need to use the following query. 因此,如果要全部使用三个,则需要使用以下查询。

 SELECT a.name as admin_name, concat(u.first_name, ' ', u.last_name) as user_name, ptc.* FROM project_task_comments ptc left join admins a on a.id= ptc.user_id and ptc.user_type_id=1 left join users u on u.id=ptc.user_id and ptc.user_type_id=2 

Generally speaking, you need to SELECT the main table (eg users) and do TWO conditional joins of the related tables. 一般来说,您需要选择主表(例如,用户),并对相关表进行两次条件连接。

This will generally mean that internally, your query will return an array of rows in the following manner: comment.* | 这通常意味着在内部,您的查询将以以下方式返回行数组:comment。* | user.* | 用户。* | admin.* 管理员*。

For some rows, you will have null columns for the user columns, for other rows you will have null columns for the admin columns. 对于某些行,用户列将具有空列,对于其他行,对于管理列,将具有空列。 In the end, you will probably need the first non-null value. 最后,您可能需要第一个非null值。

So if you have a row such as: 因此,如果您有如下一行:

comment.id | comment.text | user.username | admin.username
123 | "Example text" | null | john2019

In the end you want to escape the null and return just the john2019 . 最后,您想转义null并仅返回john2019

The MySQL function COALESCE() comes to aid: MySQL函数COALESCE()有助于:

SELECT comment.id, comment.text, COALESCE(user.username, admin.username)

It will select the first, non-null column from the provided ones, so the row selected will be: 它将从提供的列中选择第一列非空,因此选择的行将是:

123 | "Example text" | john2019

I hope you find this not too confusing. 希望您不要觉得这太令人困惑。 Let me know in a comment below if you need further clarification. 如果您需要进一步说明,请在下面的评论中告诉我。

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

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