情况那么那么来自其他表

时间:2017-11-08 05:18:21

标签: mysql

我有两张桌子。我需要第一张表中第二张表的最小值。我想填充least_t2列,其中值将来自table2最小值。

table1
id   name  d1   d2   d3
1    asd   1    2    3 
table2
id   name  c1   c2   c3   c4
1    poi   7    5    6    9

我的输出表格如下table1

id   name  d1   d2   d3   least  least_t2
1    asd   1    2    3    1        b  

我的查询如下:

select * , least(d1, d2, d3),
    (CASE least(d1, d2, d3)
        WHEN d1  THEN  (CASE least(c1, c2, c3, c4) from `table2`
                                WHEN c1  THEN 'a'
                                WHEN c2  THEN 'b'
                                WHEN c3  THEN 'c'
                                WHEN c4  THEN 'd'
                        END)
        WHEN d2  THEN  (CASE least(c1, c2, c3, c4) from `table2`
                                WHEN c1  THEN 'a'
                                WHEN c2  THEN 'b'
                                WHEN c3  THEN 'c'
                                WHEN c4  THEN 'd'
                        END)
        WHEN d3  THEN   (CASE least(c1, c2, c3, c4) from `table2`
                                WHEN c1  THEN 'a'
                                WHEN c2  THEN 'b'
                                WHEN c3  THEN 'c'
                                WHEN c4  THEN 'd'
                        END)

END) as least_t2
FROM `table1`

通过上述查询,没有显示结果。

3 个答案:

答案 0 :(得分:1)

如果可以使用table1列加入table2id,则可以执行以下查询。

select t1.*,
       least(d1, d2, d3) as least,
       case least(c1, c2, c3, c4) 
            when c1 then 'a'
            when c2 then 'b'
            when c3 then 'c'
            when c1 then 'd'
       end as  least_t2 
from table1 t1
inner join table2 t2 
on t1.id=t2.id

答案 1 :(得分:1)

检查以下代码是否可行。

select * , least(d1, d2, d3),
    (CASE least(d1, d2, d3)
        WHEN d1  THEN  (select CASE least(c1, c2, c3, c4) 
                                WHEN c1  THEN 'a'
                                WHEN c2  THEN 'b'
                                WHEN c3  THEN 'c'
                                WHEN c4  THEN 'd'
                        END from `table2` )
        WHEN d2  THEN  (select CASE least(c1, c2, c3, c4) 
                                WHEN c1  THEN 'a'
                                WHEN c2  THEN 'b'
                                WHEN c3  THEN 'c'
                                WHEN c4  THEN 'd'
                        END from `table2` )
        WHEN d3  THEN   (select CASE least(c1, c2, c3, c4) 
                                WHEN c1  THEN 'a'
                                WHEN c2  THEN 'b'
                                WHEN c3  THEN 'c'
                                WHEN c4  THEN 'd'
                        END from `table2` )

END) as least_t2
FROM `table1`

select * , least(d1, d2, d3),(select CASE least(c1, c2, c3, c4)  WHEN c1  THEN 'a' WHEN c2  THEN 'b' WHEN c3  THEN 'c' WHEN c4  THEN 'd' END from `table2`) as least_t2
FROM `table1`

答案 2 :(得分:1)

尝试此查询:

SELECT
    t1.*,
    LEAST(t1.d1, t1.d2, t1.d3) AS `least`,
    CASE WHEN t2.c1 = LEAST(t2.c1, t2.c2, t2.c3, t2.cr) THEN 'a'
         WHEN t2.c2 = LEAST(t2.c1, t2.c2, t2.c3, t2.cr) THEN 'b'
         WHEN t2.c3 = LEAST(t2.c1, t2.c2, t2.c3, t2.cr) THEN 'c'
         WHEN t2.c4 = LEAST(t2.c1, t2.c2, t2.c3, t2.cr) THEN 'd'
         ELSE 'Unknown' END AS least_t2
FROM table1 t1
LEFT JOIN table2 t2
    ON t1.id = t2.id