如何在mysql中进行嵌套选择查询

时间:2016-08-30 11:41:46

标签: 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 = 'INFO1'; 然后结果应该像这样显示。

                                    Result                              
+-----------------------------------------------------------------------------------------------+   
|   ID      | Name  | Rooms |                           RoomGuests                              |
+-----------------------------------------------------------------------------------------------+   
|   INFO1   |   ABC |   2   | [{"NoOfAdults" : "2", "NoOfChild" : "2", "ChildAge" : "[4,5]"},   |
|           |       |       |  {"NoOfAdults" : "3", "NoOfChild" : "", "ChildAge" : "[]"}]       |
+-----------------------------------------------------------------------------------------------+   

所有结果应该显示为

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

我尝试了下面的代码但是没有用。我无法理解怎么做

SELECT 
    S.`ID`, A.`Name`, A.`Rooms`, 
    CONCAT(
        '[',
            GROUP_CONCAT( 
                CONCAT(
                    '{
                        \"NoOfAdults\":\"', R.Adults,'\",
                        \"NoOfChild\":\"', R.Child,'\",
                        \"ChildAge\":
                            \"', 
                                CONCAT( 
                                    '[', 
                                        GROUP_CONCAT( 
                                            CONCAT('{',C.Age,'}')
                                        ), 
                                    ']'
                                ),
                            ,'\",
                    }'
                ) 
            ),
        ']'
    ) AS RoomGuests, 
FROM `Info_Table` AS I
LEFT JOIN `RoomGuests_Table` AS R ON R.`R_ID` = A.`ID`
LEFT JOIN `ChildAge_Table` AS C ON C.`R_ID` = R.`R_ID`
GROUP BY A.R_ID;


或者有没有像这样制作阵列的最好方法请告诉我

Array
(
    [ID] => INFO1
    [Name] => ABC
    [Rooms] => 2
    [RoomGuests] => Array
        (
            [0] => Array
                (
                    [NoOfAdults] => 2
                    [NoOfChild] => 2
                    [ChildAge] => Array
                        (
                            [0] => 4
                            [1] => 5
                        )

                )

            [1] => Array
                (
                    [NoOfAdults] => 3
                    [NoOfChild] => 0
                    [ChildAge] => Array
                        (
                        )

                )

        )

)

3 个答案:

答案 0 :(得分:1)

尝试使用此

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\":', 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
        GROUP BY RG_ID
    ) RG ON RG.RG_ID = i.ID
WHERE i.ID = INFO1;

答案 1 :(得分:0)

尽管我认为SGBD不应该参与数据的装饰,但您需要分两步完成。

首先,您需要执行group_concat来聚合child_table数据。 然后,您将能够使用RoomGuests_Table和第一个查询的结果执行主group_concat。

如果您尝试仅使用一个group_concat进行一次查询,则会有重复数据。

例如:

SELECT ...
FROM info_table it
left outer join RoomGuests_Table rt on it.ID = rt.R_ID
left outer join 
 ( SELECT ct.R_ID AS RG_ID, GROUP_CONCAT(ct.Age SEPARATOR ',') as concat_age
 FROM ChildAge_Table as ct
 GROUP BY ct.R_ID) tmp on tmp.RG_ID = rt.ID
GROUP BY ....

答案 2 :(得分:0)

希望这有帮助。

services.AddMvc(
            options => 
            options.ModelBindingMessageProvider.ValueIsInvalidAccessor = s => $"My not valid text for {s}");