MySQLでgroup_concat()関数の正しい実行に問題があります。私はデータベースにアクションの対象となるエンティティを持っています。特定のアクションには処理の詳細が必要です。処理の詳細は処理のIDとして提供されます。私の処理内容は、いくつかの主要な処理の子と、これは私のクエリでmysqlのgroup_concat関数とwhere句で 'union'と 'in'を使用する
parent_id
として表されるdBで、私は同じPARENT_IDを持つすべての子の処理によって処理されたすべてのエンティティのためのデータベースを求めているかもしれません私の開始エンティティの処理(私はすべての情報を事前に単一のエンティティを取得する)。データベースで
は、WHERE句は次のようになります。意図したとおり、私はselect句に()GROUP_CONCATを追加しようとするまで
where p.processing_id in
(SELECT processing_id FROM processing_type_1 where parent_id in
(select parent_id from processing_type_1 where parent_id in (p.processing_id)))
union all (SELECT processing_id FROM processing_type_2 where parent_id =
(select parent_id from processing_type_2 where parent_id in (p.processing_id)))
この句は動作します。エンティティの各アクションには担当者が割り当てられており、すべての人に特定のアクションを割り当てる必要があります。
select句は次のようになります。
group_concat(distinct (select z.full_name from user z join entity_action ea on z.id = ea.personnel_id where ea.entity_id = e.id and ea.action_type = 'some_action' order by z.full_name) separator ', ') as action_personnel
は、これは私がGROUP_CONCATにおけるサブクエリの終わり()で
limit 1
を追加するまで、クエリが動作していないと述べました。行動に割り当てられた人が1人しかいない場合は問題にはなりませんが、残念なことに、そうではありません。
私の質問です - これらの2つの機能を実現する方法はありますか?
私の例のデータは以下のようになります。私はすべてのprocessin約尋ねた場合
entity_table
entity_id
1
2
3
entity_actionテーブル
action_id|entity_id| action_name|personnel_id|processing_id
1| 1|'some action'| 1| 15
2| 1| 'other'| 1|
3| 1| 'another'| 2|
4| 1|'some action'| 3| 17
処理テーブル
processing_id|parent_id
15| 5
17| 5
SELECT e.entity_id,
group_concat(distinct (select z.full_name from user z join entity_action ea on z.id = ea.personnel_id where ea.entity_id = e.id and ea.action_type = 'some_action' order by z.full_name) separator ', ') as action_personnel
FROM enity e
inner join entity_action ea on ea.entity_id = e.entity_id
left outer join processing p on p.id = ea.entity_id
where
(1 IS null OR
p.processing_id in
(SELECT processing_id FROM processing_type_1 where parent_id in
(select parent_id from processing_type_1 where parent_id in (p.processing_id)))
union all (SELECT processing_id FROM processing_type_2 where parent_id =
(select parent_id from processing_type_2 where parent_id in (p.processing_id)))
)
group by e.entity_id;
私たちにサンプル表データとあなたの期待される出力を示してください。 –
これは非常に遅いクエリのようです。ネストされた 'IN'サブセレクトはおそらく私が想像できる最も遅いものです。私はクエリを再設計して、 'JOIN'を利用してより速く便利にすることができると思います。最初に改善するには、AS(エイリアス)を使用します。 – DanFromGermany
完全なクエリを表示できますか?あなたがしようとしていることは、GROUP_CONCATの結果に対してINを使用することだと思います。 GROUP_CONCATは値のリストではなく文字列を返すため、これは機能しません。 – Kickstart