Skip to main content
本文に 44 文字追加
ソース リンク
Yuki Inoue
  • 1.7万
  • 19
  • 89
  • 208

タグの名前が重複していようが、無理やり表示する別解としてこちらを残しておきます。

JSON_ARRAYAGG (5.7.22 から導入) を用いて、

select id, agg->'$[*][0]'  as tag_ids, agg->'$[*][1]' as tags from (
  select b.id, JSON_ARRAYAGG(JSON_ARRAY(t.tag_id, t.tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    LEFT JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    LEFT JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = 1
  ) as t
  group by b.id
) as t;

そして、自分でしたら、 json 配列を 2,5 などに整形する必要がある場合、それはアプリケーション側で実行すると思います。


もし、 blog_tag に重複エントリがある場合には、一回 distinct をかけます。具体的には:

select id, agg->'$[*][0]'  as tag_ids, agg->'$[*][1]' as tags from (
  select id, JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    LEFT JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    LEFT JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = 1
  ) as t
  group by id
) as t;

JSON_ARRAYAGG (5.7.22 から導入) を用いて、

select id, agg->'$[*][0]' as tag_ids, agg->'$[*][1]' as tags from (
  select b.id, JSON_ARRAYAGG(JSON_ARRAY(t.tag_id, t.tag)) as agg
  FROM
   blog AS b
  LEFT JOIN blog_tag AS bt ON (b.id = bt.blog_id)
  LEFT JOIN tag AS t ON (t.tag_id = bt.tag_id)
  where b.id = 1
  group by b.id
) as t;

そして、自分でしたら、 json 配列を 2,5 などに整形する必要がある場合、それはアプリケーション側で実行すると思います。


もし、 blog_tag に重複エントリがある場合には、一回 distinct をかけます。具体的には:

select id, agg->'$[*][0]'  as tag_ids, agg->'$[*][1]' as tags from (
  select id, JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    LEFT JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    LEFT JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = 1
  ) as t
  group by id
) as t;

タグの名前が重複していようが、無理やり表示する別解としてこちらを残しておきます。

JSON_ARRAYAGG (5.7.22 から導入) を用いて、

select id, agg->'$[*][0]'  as tag_ids, agg->'$[*][1]' as tags from (
  select id, JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    LEFT JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    LEFT JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = 1
  ) as t
  group by id
) as t;

そして、自分でしたら、 json 配列を 2,5 などに整形する必要がある場合、それはアプリケーション側で実行すると思います。

Yuki Inoueによって削除が取り消された投稿
削除済み投稿 (投票者: Yuki Inoue
本文に 22 文字追加
ソース リンク
Yuki Inoue
  • 1.7万
  • 19
  • 89
  • 208
with target_id as ( select 3 ),  -- ここを実際のプレースホルダにかえる
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = (select * from target_id)
  ) as t
  group by id
)
select (select * from target_id) as id,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' from agg),
    JSON_ARRAY()
  ) as tags
with target_id as ( select 3 ),
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = (select * from target_id)
  ) as t
  group by id
)
select (select * from target_id) as id,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' from agg),
    JSON_ARRAY()
  ) as tags
with target_id as ( select 3 ),  -- ここを実際のプレースホルダにかえる
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = (select * from target_id)
  ) as t
  group by id
)
select (select * from target_id) as id,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' from agg),
    JSON_ARRAY()
  ) as tags
本文に 1345 文字追加
ソース リンク
Yuki Inoue
  • 1.7万
  • 19
  • 89
  • 208

MySQLただ、ここまでだと tag に紐づかない blog については、

+----+---------+--------+
| id | tag_ids | tags   |
+----+---------+--------+
|  3 | [null]  | [null] |
+----+---------+--------+

が得られてしまいます。

8.0 以上をお持ち場合、 CTE で検証を簡単を用いれば、以下のよう行えるクエリを貼っておきできます。

with target_id as ( select 3 ),
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = (select * from target_id)
  ) as t
  group by id
)
select (select * from target_id) as id,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' from agg),
    JSON_ARRAY()
  ) as tags
+------+---------+------+
| id   | tag_ids | tags |
+------+---------+------+
|    3 | []      | []   |
+------+---------+------+

以下、手軽に検証するための CTE

with blog as (
  select 1 as id
  union all
  select 2 as id
  union all
  select 3 as id
), tag as (
  select 2 as tag_id, 'y' as tag
  union all
  select 5, 'x'
  union all
  select 1, 'z'
), blog_tag as (
  select 1 as blog_id, 2 as tag_id
  union all
  select 1 as blog_id, 2 as tag_id
  union all
  select 1 as blog_id, 5 as tag_id
  union all
  select 2 as blog_id, 1 as tag_id
),
-- 以下、実際のクエリ部分
target_id as ( select id3), agg->'$[*][0]'- ここを実際のプレースホルダにかえる
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_ids_id, agg->'$[*][1]'tag)) as tagsagg
  from (
    select distinct b.id, JSON_ARRAYAGG(JSON_ARRAY(t.tag_id, t.tag)) 
 as agg
  FROM
     blog AS b
LEFT    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
LEFT    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = 1(select * from target_id)
  ) as t
  group by b.id
)
select (select * from target_id) as tid,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' as tags from agg),
    JSON_ARRAY()
  ) as tags;

MySQL 8 以上をお持ち場合、 CTE で検証を簡単行えるクエリを貼っておきます。

with blog as (
  select 1 as id
  union all
  select 2 as id
), tag as (
  select 2 as tag_id, 'y' as tag
  union all
  select 5, 'x'
  union all
  select 1, 'z'
), blog_tag as (
  select 1 as blog_id, 2 as tag_id
  union all
  select 1 as blog_id, 5 as tag_id
  union all
  select 2 as blog_id, 1 as tag_id
)

select id, agg->'$[*][0]'  as tag_ids, agg->'$[*][1]' as tags from (select b.id, JSON_ARRAYAGG(JSON_ARRAY(t.tag_id, t.tag)) as agg
FROM
 blog AS b
LEFT JOIN blog_tag AS bt ON (b.id = bt.blog_id)
LEFT JOIN tag AS t ON (t.tag_id = bt.tag_id)
where b.id = 1
group by b.id) as t;

ただ、ここまでだと tag に紐づかない blog については、

+----+---------+--------+
| id | tag_ids | tags   |
+----+---------+--------+
|  3 | [null]  | [null] |
+----+---------+--------+

が得られてしまいます。

8.0 以上の CTE を用いれば、以下のようできます。

with target_id as ( select 3 ),
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag
    FROM
     blog AS b
    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = (select * from target_id)
  ) as t
  group by id
)
select (select * from target_id) as id,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' from agg),
    JSON_ARRAY()
  ) as tags
+------+---------+------+
| id   | tag_ids | tags |
+------+---------+------+
|    3 | []      | []   |
+------+---------+------+

以下、手軽に検証するための CTE

with blog as (
  select 1 as id
  union all
  select 2 as id
  union all
  select 3 as id
), tag as (
  select 2 as tag_id, 'y' as tag
  union all
  select 5, 'x'
  union all
  select 1, 'z'
), blog_tag as (
  select 1 as blog_id, 2 as tag_id
  union all
  select 1 as blog_id, 2 as tag_id
  union all
  select 1 as blog_id, 5 as tag_id
  union all
  select 2 as blog_id, 1 as tag_id
),
-- 以下、実際のクエリ部分
target_id as ( select 3), -- ここを実際のプレースホルダにかえる
agg as (
  select JSON_ARRAYAGG(JSON_ARRAY(tag_id, tag)) as agg
  from (
    select distinct b.id, t.tag_id, t.tag 
    FROM
     blog AS b
    INNER JOIN blog_tag AS bt ON (b.id = bt.blog_id)
    INNER JOIN tag AS t ON (t.tag_id = bt.tag_id)
    where b.id = (select * from target_id)
  ) as t
  group by id
)
select (select * from target_id) as id,
  coalesce(
    (select agg->'$[*][0]' from agg),
    JSON_ARRAY()
  ) as tag_ids,
  coalesce(
    (select agg->'$[*][1]' as tags from agg),
    JSON_ARRAY()
  ) as tags;
本文に 441 文字追加
ソース リンク
Yuki Inoue
  • 1.7万
  • 19
  • 89
  • 208
読み込んでいます
本文に 68 文字追加
ソース リンク
Yuki Inoue
  • 1.7万
  • 19
  • 89
  • 208
読み込んでいます
ソース リンク
Yuki Inoue
  • 1.7万
  • 19
  • 89
  • 208
読み込んでいます