gpt4 book ai didi

mysql - 如何在mysql中使用带左连接的if条件

转载 作者:可可西里 更新时间:2023-11-01 06:50:49 28 4
gpt4 key购买 nike

我有下面的表格,它们像这样相互连接

Info_Table -> RoomGuests_Table -> ChildAge_Table

这些是表格

        Info_Table          
+---------------------------+
| ID | Name | Rooms |
+---------------------------+
| INFO1 | ABC | 2 |
| INFO2 | DEF | 1 |
| INFO3 | GHI | 3 |
+---------------------------+


RoomGuests_Table
+-----------------------------------+
| ID | R_ID | Adult | Child |
+-----------------------------------+
| RG1 | INFO1 | 2 | 2 |
| RG2 | INFO1 | 3 | 0 |
| RG3 | INFO2 | 2 | 1 |
| RG4 | INFO3 | 2 | 1 |
| RG5 | INFO3 | 2 | 2 |
| RG6 | INFO3 | 2 | 1 |
+-----------------------------------+


ChildAge_Table
+-----------------------+
| ID | R_ID | Age |
+-----------------------+
| CA1 | RG1 | 4 |
| CA2 | RG1 | 5 |
| CA3 | RG3 | 2 |
| CA4 | RG4 | 7 |
| CA5 | RG5 | 1 |
| CA6 | RG5 | 5 |
| CA7 | RG6 | 3 |
+-----------------------+

我想要这样的结果

如果 Info_Table 的 ID = 'INFO3';那么结果应该是这样显示的。

                                    Result                              
+-----------------------------------------------------------------------------------------------+
| ID | Name | Rooms | RoomGuests |
+-----------------------------------------------------------------------------------------------+
| INFO3 | GHI | 3 | [{ "NoOfAdults":"2", "NoOfChild":"1", "ChildAge":[7] }, |
| | | | { "NoOfAdults":"2", "NoOfChild":"2", "ChildAge":[1,5] }, |
| | | | { "NoOfAdults":"2", "NoOfChild":"1", "ChildAge":[3] }] |
+-----------------------------------------------------------------------------------------------+

我已经尝试了下面的代码并且有效。

SELECT i.ID, i.name,i.rooms, RG.RoomGuests
FROM Info_Table i
LEFT JOIN (
SELECT
R.ID, R.R_ID AS RG_ID,
CONCAT(
'[',
GROUP_CONCAT(
CONCAT(
'{
\"NoOfAdults\":\"', Adult,'\",
\"NoOfChild\":\"', Child,'\",
\"ChildAge\":', CA.ChildAge,'
}'
)
),
']'
) AS RoomGuests

FROM RoomGuests_Table R

LEFT JOIN (
SELECT
C.R_ID AS CA_ID,
CONCAT(
'[',
GROUP_CONCAT( Age SEPARATOR ','),
']'
) AS ChildAge
FROM ChildAge_Table C
GROUP BY CA_ID
) CA ON CA.CA_ID = R.ID)

GROUP BY RG_ID

) RG ON RG.RG_ID = i.ID
WHERE i.ID = 'INFO3';

但在以下情况下不起作用

如果这样记录

        Info_Table          
+---------------------------+
| ID | Name | Rooms |
+---------------------------+
| INFO3 | GHI | 3 |
+---------------------------+


RoomGuests_Table
+-----------------------------------+
| ID | R_ID | Adult | Child |
+-----------------------------------+
| RG4 | INFO3 | 2 | 0 |
| RG5 | INFO3 | 2 | 2 |
| RG6 | INFO3 | 2 | 1 |
+-----------------------------------+


ChildAge_Table
+-----------------------+
| ID | R_ID | Age |
+-----------------------+
| CA5 | RG5 | 1 |
| CA6 | RG5 | 5 |
| CA7 | RG6 | 3 |
+-----------------------+

如您所见,RoomGuests_TableRG4 没有 child 意味着它有 0 值,所以在这种情况下它应该显示这样的结果

                                Result                              
+-----------------------------------------------------------------------------------------------+
| ID | Name | Rooms | RoomGuests |
+-----------------------------------------------------------------------------------------------+
| INFO3 | GHI | 3 | [{ "NoOfAdults":"2", "NoOfChild":"0", "ChildAge":[] }, |
| | | | { "NoOfAdults":"2", "NoOfChild":"2", "ChildAge":[1,5] }, |
| | | | { "NoOfAdults":"2", "NoOfChild":"1", "ChildAge":[3] }] |
+-----------------------------------------------------------------------------------------------+

但是显示是这样的

                                    Result                              
+-----------------------------------------------------------------------------------------------+
| ID | Name | Rooms | RoomGuests |
+-----------------------------------------------------------------------------------------------+
| INFO3 | GHI | 3 | [{ "NoOfAdults":"2", "NoOfChild":"2", "ChildAge":[1,5] }, |
| | | | { "NoOfAdults":"2", "NoOfChild":"1", "ChildAge":[3] }] |
+-----------------------------------------------------------------------------------------------+

ChildAge_Table 没有 RG4 的记录,所以如何得到我想要的结果或有没有办法检查 CA.ChildAge = null 然后放在那里 '[]'

最佳答案

尝试使用 IFNULL 保护 CA.ChildAge,如下所示:

...CONCAT(
'{\"NoOfAdults\":\"', Adult,'\",
\"NoOfChild\":\"', Child,'\",
\"ChildAge\":', IFNULL(CA.ChildAge, '[]'),'}'
)...

问题是 C.ChildAge 对于“RG4”(您知道)不存在,因此作为 NULL CA 左连接到 RoomGuests_Table。 child 年龄。此 NULL 也使内部 CONCAT('{...}') NULL,但是 NULLs are filtered out by GROUP_CONCAT ,这意味着外部 CONCAT('[...]') 将看不到它。

这是完整的、有效的 SQL:

   SELECT i.ID, i.name,i.rooms, RG.RoomGuests
FROM Info_Table i
LEFT JOIN ( SELECT R.ID, R.R_ID AS RG_ID,
CONCAT('[',
GROUP_CONCAT(CONCAT('{\"NoOfAdults\":\"', Adult,'\",\n',
'\"NoOfChild\":\"', Child,'\",\n',
'\"ChildAge\":', IFNULL(CA.ChildAge, '[]'),'}')),
']')
AS RoomGuests
FROM RoomGuests_Table R
LEFT JOIN ( SELECT C.R_ID AS CA_ID,
CONCAT('[',
GROUP_CONCAT( Age SEPARATOR ','),
']') AS ChildAge
FROM ChildAge_Table C
GROUP BY CA_ID) CA
ON CA.CA_ID = R.ID) RG
ON RG.RG_ID = i.ID
WHERE i.ID = 'INFO3'
GROUP BY RG_ID;

关于mysql - 如何在mysql中使用带左连接的if条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39273808/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com