在我的table1
中有这样的日期:
id value
1 500
2 400
3 300
4 200
5 100
我想在显示表中设置上面的数据:
d1 d2 d3 d4 d5
500 400 300 200 100
我正在使用以下 SQL 查询。
"SELECT `value` as d1 from `table1` WHERE `id`=1"
"SELECT `value` as d2 from `table1` WHERE `id`=2"
"SELECT `value` as d3 from `table1` WHERE `id`=3"
"SELECT `value` as d4 from `table1` WHERE `id`=4"
"SELECT `value` as d5 from `table1` WHERE `id`=5"
如何在单个 SQL 查询中进行所有查询?
最佳答案
我会使用聚合:
select max(case when id = 1 then value end) as d1,
max(case when id = 2 then value end) as d2,
max(case when id = 3 then value end) as d3,
max(case when id = 4 then value end) as d4,
max(case when id = 5 then value end) as d5
from t;
关于php - 如何在一个sql查询中多次使用where条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47298827/