简体   繁体   English

合并2个SQL结果中的数组

[英]Merge arrays from 2 SQL results

So i have a tags table setup in SQL 所以我在SQL中设置了标签表

lists, lists_tags, tags 列表,lists_tags,标签

Each list can have multiple tags, and tags have two types genre and producer. 每个列表可以具有多个标签,并且标签具有类型和生产者两种类型。 Now if we are looking for all lists with tags 'action' these are the steps im following 现在,如果我们正在寻找带有“ action”标签的所有列表,这些是我遵循的步骤

SELECT GROUP_CONCAT(mini_lists_tags.list_id) AS list_ids
FROM (`mini_tags`)
LEFT JOIN `mini_lists_tags` ON `mini_lists_tags`.`tag_id` = `mini_tags`.`tag_id`
WHERE `mini_tags`.`tag_slug` = 'action'  

That will return an array 1,2 for the ids of the list. 这将为列表的ID返回一个数组1,2。

SELECT *
FROM (`mini_lists_anime`)
JOIN `mini_lists` ON `mini_lists`.`list_id` = `mini_lists_anime`.`list_id`
WHERE `mini_lists`.`list_id` IN ('1', '2') 
AND `mini_lists`.`list_state` = 'active'

that gets all the lists in an array EXAMPLE: 将所有列表放入数组中示例:

Array
(
    [0] => stdClass Object
        (
            [list_id] => 1
            [list_episodes] => 13
            [list_duration] => 24
            [list_aired] => 1238623200
            [list_age_rate] => PG-13 - Teens 13 or older
            [user_id] => 1
            [list_mal] => 5342
            [list_category] => Anime
            [list_type] => TV
            [list_status] => Completed
            [list_title] => Asura Cryin'
            [list_alt_titles] => アスラクライン
            [list_thumb] => 17071
            [list_likes] => 0
            [list_date] => 1300609723
            [list_update] => 0
            [list_state] => active
            [list_info] => 
        )

    [1] => stdClass Object
        (
            [list_id] => 2
            [list_episodes] => 26
            [list_duration] => 23
            [list_aired] => 1238623200
            [list_age_rate] => PG-13 - Teens 13 or older
            [user_id] => 1
            [list_mal] => 329
            [list_category] => Anime
            [list_type] => TV
            [list_status] => Completed
            [list_title] => Planetes
            [list_alt_titles] => プラネテス
            [list_thumb] => 4822
            [list_likes] => 0
            [list_date] => 1300609723
            [list_update] => 0
            [list_state] => active
            [list_info] => 
        )

)

And then we get the tags 然后我们得到标签

SELECT `mini_lists_tags`.`list_id`, `mini_tags`.`tag_type`, GROUP_CONCAT(mini_tags.tag_name) AS tag_names
FROM (`mini_lists_tags`)
INNER JOIN `mini_tags` ON `mini_tags`.`tag_id` = `mini_lists_tags`.`tag_id`
WHERE `mini_lists_tags`.`list_id` IN ('1', '2') 
GROUP BY `mini_lists_tags`.`list_id`, `mini_tags`.`tag_type`  

that gets all the tags in an array EXAMPLE: 将所有标签放入数组中示例:

Array
(
    [0] => stdClass Object
        (
            [list_id] => 1
            [tag_type] => Genre
            [tag_names] => Supernatural,Action,Mecha
        )

    [1] => stdClass Object
        (
            [list_id] => 1
            [tag_type] => Producers
            [tag_names] => Seven Arcs
        )

    [2] => stdClass Object
        (
            [list_id] => 2
            [tag_type] => Genre
            [tag_names] => Romance,Action,Sci-fi,Comedy,Slice of Life,Drama,Space
        )

    [3] => stdClass Object
        (
            [list_id] => 2
            [tag_type] => Producers
            [tag_names] => Sunrise,Bandai Entertainment,Bandai Visual,Bang Zoom! Entertainment
        )

)

Now the problem is I need to get them merged on the list_id so it returns something like this for each one. 现在的问题是,我需要将它们合并到list_id上,以便为每个列表返回类似的内容。

    stdClass Object
    (
        [list_id] => 1
        [list_episodes] => 13
        [list_duration] => 24
        [list_aired] => 1238623200
        [list_age_rate] => PG-13 - Teens 13 or older
        [user_id] => 1
        [list_mal] => 5342
        [list_category] => Anime
        [list_type] => TV
        [list_status] => Completed
        [list_title] => Asura Cryin'
        [list_alt_titles] => アスラクライン
        [list_thumb] => 17071
        [list_likes] => 0
        [list_date] => 1300609723
        [list_update] => 0
        [list_state] => active
        [list_info] => 
        [list_tags] => Array
            (
                [0] => stdClass Object
                    (
                        [tag_type] => Genre
                        [tag_names] => Mecha,Action,Supernatural
                    )

                [1] => stdClass Object
                    (
                        [tag_type] => Producers
                        [tag_names] => Seven Arcs
                    )

            )

)

Any advice is appreciated, i'm really lost. 任何建议表示赞赏,我真的迷路了。 if there is better solution than this, i am all ears. 如果有比这更好的解决方案,我全神贯注。

You can do another type of join that will return the parent item multiple times merged with each child object like so: 您可以执行另一种类型的联接,该联接将多次将父项与每个子对象合并,如下所示:

Array(
   [0] => stdClass Object
    (
        [list_id] => 1
        [list_episodes] => 13
        [list_duration] => 24
        ...etc
        [tag_type] => Genre
        [tag_names] => Supernatural,Action,Mecha
        ...etc
    )
   [1] => stdClass Object
    (
        [list_id] => 1
        [list_episodes] => 13
        [list_duration] => 24
        ...etc
        [tag_type] => Producers
        [tag_names] => Seven Arcs
        ...etc
    )
   [2] => stdClass Object
    (
        [list_id] => 2
        [list_episodes] => 26
        [list_duration] => 23
        ...etc
        [tag_type] => Genre
        [tag_names] => Supernatural,Action,Mecha
        ...etc
    )
   [3] => stdClass Object
    (
        [list_id] => 2
        [list_episodes] => 26
        [list_duration] => 23
        ...etc
        [tag_type] => Producers
        [tag_names] => Seven Arcs
        ...etc
    )
)

You will then need to loop through your results merging down the results into their child/parent relationships. 然后,您将需要遍历结果,将结果合并为他们的子/父母关系。 This is because SQL always returns rows as results, not complex structures. 这是因为SQL总是返回行作为结果,而不是复杂的结构。

Although this is more complex to deal with, it's ususally less process intensive than making looped sql queries for each parent object (known as n+1 queries) 尽管处理起来比较复杂,但是与为每个父对象进行循环sql查询(通常称为n + 1查询)相比,它通常耗费较少的过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM