【发布时间】:2012-09-27 13:40:19
【问题描述】:
我有许多表,它们之间通过外键和主键(基本)系统相关联。
我想根据某些条件连接(逗号分隔)最后一个“深入”表的值。
更大的图片可以在这里找到:http://tinypic.com/r/2ngdaah/6
注意:tableE 已经创建,列名对应于 tableD
中显示的名称谢谢!
已编辑(小提琴):
【问题讨论】:
标签: mysql inner-join group-concat
我有许多表,它们之间通过外键和主键(基本)系统相关联。
我想根据某些条件连接(逗号分隔)最后一个“深入”表的值。
更大的图片可以在这里找到:http://tinypic.com/r/2ngdaah/6
注意:tableE 已经创建,列名对应于 tableD
中显示的名称谢谢!
【问题讨论】:
标签: mysql inner-join group-concat
我做了一个查询,但我不确定是否正确。原因是您描述的条件使用了符号
但是,试试这个:
select *
from (select id,
name,
max(case when e.tableD_id = 1 then e.agg_values else null end) alpha,
max(case when e.tableD_id = 2 then e.agg_values else null end) beta,
max(case when e.tableD_id = 3 then e.agg_values else null end) gamma
from tableA a,
(select b.tableA_id,
b.tableD_id,
group_concat(cast(c.value as char) order by c.id asc) agg_values
from tableB b,
tableC c
where c.tableB_id = b.id
group by b.tableD_id,b.tableA_id
having count(*) >= 3) e
where a.id = e.tableA_id
group by id, name
) n
where n.alpha is not null
and n.beta is not null
and n.gamma is not null
编辑:
修改了查询以支持使用 >= 而不是
SQLFIDDLE:http://sqlfiddle.com/#!2/f28c8/1/0
SQLFIDDLE,仅第一个条件:http://sqlfiddle.com/#!2/f28c8/4
【讨论】: