【问题标题】:Data transposition and concatenation, how to follow inner join condition?数据转置和连接,如何遵循内连接条件?
【发布时间】:2012-09-27 13:40:19
【问题描述】:

我有许多表,它们之间通过外键和主键(基本)系统相关联。

我想根据某些条件连接(逗号分隔)最后一个“深入”表的值。

因为很难解释,所以我做了一个平局,我认为它非常明确:

更大的图片可以在这里找到:http://tinypic.com/r/2ngdaah/6

注意:tableE 已经创建,列名对应于 tableD

中显示的名称

谢谢!

已编辑(小提琴):

http://sqlfiddle.com/#!2/f28c8/1/0

【问题讨论】:

    标签: mysql inner-join group-concat


    【解决方案1】:

    我做了一个查询,但我不确定是否正确。原因是您描述的条件使用了符号

    但是,试试这个:

    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

    【讨论】:

    • 我的帖子现在被编辑了!我会检查你的答案。非常感谢!
    • 你创建的value列是int,应该是double,如果你选择tableC(select * from tableC),你会看到value列总是0。
    • 你能不能再换一次。对不起,我想我搞砸了你的改变
    • 如果我只希望条件 1 为真(忘记条件 2 或 3),我该怎么做?如果我只希望条件 2 为真(忘记条件 1 或 3),我该怎么做?
    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2019-08-03
    • 2021-08-09
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    相关资源
    最近更新 更多