mysql INNER JOIN 但不存在时也为 NULL

mysql INNER JOIN but also NULL when not exist

好的,

我一整天都在努力完成这项工作,但我在 MySQL 方面的知识似乎太有限了。

我有以下表格:

time_entries
|id|comment|ticket_id|
| 1|foo    |        1|
| 2|bar    |        1|
| 3|baz    |        2|
| 4|lorem  |        3|
| 5|ipsum  |        4|

ticket
|id|name   |
| 1|ticket1|
| 2|ticket2|
| 3|ticket3|
| 4|ticket4|


custom_fields
|id|name   |
| 1|custom1|
| 2|custom2|
| 3|custom3|

custom_values
|id|custom_field_id|ticket_id|value|
| 1|              1|        1|   22|
| 2|              2|        1|   33|
| 3|              3|        1|   44|
| 4|              1|        2|   55|
| 5|              3|        2|   66|
| 6|              2|        3|   77|
| 7|              1|        4|   88|

我想要得到的是每个 time_entry 一行,其中包含票证信息和自定义值。如果此工单未设置自定义值,则结果中的值select必须为空或为空:

select
|time_entries_comment|ticket_name|custom1_value|custom2_value|custom3_value|
|                 foo|    ticket1|           22|           33|           44|
|                 bar|    ticket1|           22|           33|           44|
|                 baz|    ticket2|           55|         NULL|           66|
|               lorem|    ticket3|         NULL|           77|         NULL|
|               ipsum|    ticket4|           88|         NULL|         NULL|

到目前为止我得到的是这样的:

select 
  te.comment,
  t.name,
  cv1.value,
  cv2.value,
  cv3.value

  from time_entries te

  LEFT JOIN ticket t ON te.ticket_id = i.id

  LEFT JOIN custom_values cv1 ON t.id = cv1.ticket_id
  LEFT JOIN custom_fields cf1 ON cv1.custom_field_id = cf1.id AND cf1.id = 1

  LEFT JOIN custom_values cv2 ON t.id = cv2.ticket_id
  LEFT JOIN custom_fields cf2 ON cv2.custom_field_id = cf2.id AND cf2.id = 2

  LEFT JOIN custom_values cv3 ON t.id = cv3.ticket_id
  LEFT JOIN custom_fields cf3 ON cv3.custom_field_id = cf3.id AND cf3.id = 3

  WHERE t.id = 1;

但这给了我所有匹配。

我尝试了内部连接,但是如果这张票没有 custom_value,我就没有结果。我也尝试过使用 UNION Left 和 Right 进行 Outer Join,但没有成功。

有什么建议吗?这里的关键词是什么?

感谢您的帮助

我认为 LEFT JOIN 是可行的方法,因为它允许您获取 NULL 值。您只需要在末尾添加一个 GROUP BY 语句

GROUP BY t.id

你想要的是一个进程调用枢轴。 这主要是通过 GROUP BY 和 MAX 函数完成的。

SELECT 
   time_entries.comment AS time_entries_comment
 , ticket.name
 , MAX(CASE WHEN custom_values.custom_field_id = 1 THEN custom_values.value ELSE NULL END) AS custom1_value
 , MAX(CASE WHEN custom_values.custom_field_id = 2 THEN custom_values.value ELSE NULL END) AS custom2_value
 , MAX(CASE WHEN custom_values.custom_field_id = 3 THEN custom_values.value ELSE NULL END) AS custom3_value
FROM 
 time_entries

INNER JOIN 
 ticket
ON
  time_entries.ticket_id = ticket.id

INNER JOIN
 custom_values  
ON
  time_entries.ticket_id = custom_values.ticket_id 

GROUP BY
    time_entries.comment
  , ticket.name

ORDER BY
  time_entries.id ASC

结果

time_entries_comment  name     custom1_value  custom2_value  custom3_value  
--------------------  -------  -------------  -------------  ---------------
foo                   ticket1             22             33               44
bar                   ticket1             22             33               44
baz                   ticket2             55         (NULL)               66
lorem                 ticket3         (NULL)             77           (NULL)
ipsum                 ticket4             88         (NULL)           (NULL)