将SQL查询与当前查询值进行比较

时间:2016-11-21 15:44:09

标签: sql oracle

如何将D1与D2进行比较,并将结果用作查询的一部分。

Select A.id,
CASE 
WHEN D1 >= D2 
THEN D2
WHEN D2 > D1
THEN D1
END
FROM A,E

我想得到D1和D2的较低者

main/0

3 个答案:

答案 0 :(得分:1)

查看LEAST function

SELECT id, LEAST(d1, d2) FROM (
    Select A.id,
    (Select b.date from b,c where b.id=c.id and c.id = E.id and b.id = 1) as d1,
    (Select b.date from b,c where b.id=c.id and c.id = E.id and b.id = 2)) as d2
    from 
    A,E 
)

答案 1 :(得分:1)

我想你可能想要这样的东西:

Select A.id, min(b.date)
from A
cross join b
join c on b.id = c.id
join E on c.id = E.id
where b in (1,2) 
group by A.id

答案 2 :(得分:0)

使用$sql1 = "SELECT * FROM table1"; $sql2 = "SELECT * FROM table2"; $sql3 = "SELECT * FROM table3"; getElements(["table1"=> $sql1, "table2" => $sql2, "table3" => $sql3]);

sub-select

开始使用select id, CASE WHEN D1 >= D2 THEN D2 ELSE D1 END From ( /* Your Query */ ) A 语法而不是旧式逗号分隔连接。这是正确的方法

JOIN