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

时间:2016-09-01 14:09:30

标签: mysql sql

我有下面的表格,它们相互连接,如

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';

但在以下情况下无效

IF如此记录

        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_Table RG4没有孩子意味着它有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然后放'[]'

2 个答案:

答案 0 :(得分:1)

尝试使用T保护CA.ChildAge,如此:

IFNULL

问题是{RG4&#39}没有...CONCAT( '{\"NoOfAdults\":\"', Adult,'\", \"NoOfChild\":\"', Child,'\", \"ChildAge\":', IFNULL(CA.ChildAge, '[]'),'}' )... 。 (你知道),因此作为NULL C.ChildAge LEFT JOIN到RoomGuests_Table。这个NULL也使内部CA.ChildAge为空,但是official documentation,意味着外部CONCAT('{...}')不会看到它。

这是一个完整的,有效的SQL:

CONCAT('[...]')

答案 1 :(得分:0)

两种方法;我还没有验证过任何一个:

计划A:使用括号表示

FROM ... LEFT JOIN ( ... LEFT JOIN ... )

FROM ( ... LEFT JOIN  ... ) LEFT JOIN ...

(我不知道哪种方式是默认值;我怀疑默认值会给您带来麻烦。

计划B:摆脱第二个左连接。将CA.ChildAge替换为构成第二次加入的SELECT。 (务必将其加以括号。)