Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge arrays from 2 SQL results
    text
    copied!<p>So i have a tags table setup in SQL</p> <p>lists, lists_tags, tags</p> <p>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</p> <pre><code>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' </code></pre> <p>That will return an array 1,2 for the ids of the list.</p> <pre><code>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' </code></pre> <p>that gets all the lists in an array EXAMPLE:</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [list_id] =&gt; 1 [list_episodes] =&gt; 13 [list_duration] =&gt; 24 [list_aired] =&gt; 1238623200 [list_age_rate] =&gt; PG-13 - Teens 13 or older [user_id] =&gt; 1 [list_mal] =&gt; 5342 [list_category] =&gt; Anime [list_type] =&gt; TV [list_status] =&gt; Completed [list_title] =&gt; Asura Cryin' [list_alt_titles] =&gt; アスラクライン [list_thumb] =&gt; 17071 [list_likes] =&gt; 0 [list_date] =&gt; 1300609723 [list_update] =&gt; 0 [list_state] =&gt; active [list_info] =&gt; ) [1] =&gt; stdClass Object ( [list_id] =&gt; 2 [list_episodes] =&gt; 26 [list_duration] =&gt; 23 [list_aired] =&gt; 1238623200 [list_age_rate] =&gt; PG-13 - Teens 13 or older [user_id] =&gt; 1 [list_mal] =&gt; 329 [list_category] =&gt; Anime [list_type] =&gt; TV [list_status] =&gt; Completed [list_title] =&gt; Planetes [list_alt_titles] =&gt; プラネテス [list_thumb] =&gt; 4822 [list_likes] =&gt; 0 [list_date] =&gt; 1300609723 [list_update] =&gt; 0 [list_state] =&gt; active [list_info] =&gt; ) ) </code></pre> <p>And then we get the tags</p> <pre><code>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` </code></pre> <p>that gets all the tags in an array EXAMPLE:</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [list_id] =&gt; 1 [tag_type] =&gt; Genre [tag_names] =&gt; Supernatural,Action,Mecha ) [1] =&gt; stdClass Object ( [list_id] =&gt; 1 [tag_type] =&gt; Producers [tag_names] =&gt; Seven Arcs ) [2] =&gt; stdClass Object ( [list_id] =&gt; 2 [tag_type] =&gt; Genre [tag_names] =&gt; Romance,Action,Sci-fi,Comedy,Slice of Life,Drama,Space ) [3] =&gt; stdClass Object ( [list_id] =&gt; 2 [tag_type] =&gt; Producers [tag_names] =&gt; Sunrise,Bandai Entertainment,Bandai Visual,Bang Zoom! Entertainment ) ) </code></pre> <p>Now the problem is I need to get them merged on the list_id so it returns something like this for each one.</p> <pre><code> stdClass Object ( [list_id] =&gt; 1 [list_episodes] =&gt; 13 [list_duration] =&gt; 24 [list_aired] =&gt; 1238623200 [list_age_rate] =&gt; PG-13 - Teens 13 or older [user_id] =&gt; 1 [list_mal] =&gt; 5342 [list_category] =&gt; Anime [list_type] =&gt; TV [list_status] =&gt; Completed [list_title] =&gt; Asura Cryin' [list_alt_titles] =&gt; アスラクライン [list_thumb] =&gt; 17071 [list_likes] =&gt; 0 [list_date] =&gt; 1300609723 [list_update] =&gt; 0 [list_state] =&gt; active [list_info] =&gt; [list_tags] =&gt; Array ( [0] =&gt; stdClass Object ( [tag_type] =&gt; Genre [tag_names] =&gt; Mecha,Action,Supernatural ) [1] =&gt; stdClass Object ( [tag_type] =&gt; Producers [tag_names] =&gt; Seven Arcs ) ) ) </code></pre> <p>Any advice is appreciated, i'm really lost. if there is better solution than this, i am all ears.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload