mysql INNER JOIN但不存在时也为NULL

时间:2017-05-03 14:28:17

标签: mysql join left-join inner-join outer-join

好的,

我试图让这一整天都完成,但我对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
|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 and Right的外部加入但没有成功。

有什么建议可以寻找吗?这里有什么关键词?

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

您想要的是流程调用枢轴。 这主要通过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)

答案 1 :(得分:0)

我认为LEFT JOIN是要走的路,因为它允许你获得NULL值。您只需在末尾添加GROUP BY语句

GROUP BY t.id