gpt4 book ai didi

mysql - 在 MySQL 中操作字段的最有效方法是什么?

转载 作者:行者123 更新时间:2023-11-28 23:32:49 24 4
gpt4 key购买 nike

我有以下查询:

SELECT DATE(utimestamp) as utimestamp, name, data*2000000 from tData 
where utimestamp BETWEEN '2016-01-01 00:00:00' AND '2016-04-16 00:00:00'
AND name = 'Valor2' and data>20
group by YEAR(utimestamp), MONTH(utimestamp), name
union
SELECT DATE(utimestamp) as utimestamp, name, data*0.1 from tData
where utimestamp BETWEEN '2016-01-01 00:00:00' AND '2016-04-16 00:00:00'
AND name = 'Valor1' and data>20
group by YEAR(utimestamp), MONTH(utimestamp), name
order by utimestamp asc

是否有更有效的“数据”操作方式?有没有不使用 UNION 就可以做到这一点的方法?

最佳答案

你可以尝试使用case when then:

SELECT DATE(utimestamp) as utimestamp, name, 
case when name = 'Valor1' then data*0.1
when name = 'Valor2' then data*2000000
end
from tData
where utimestamp BETWEEN '2016-01-01 00:00:00' AND '2016-04-16 00:00:00'
and data>20
group by YEAR(utimestamp), MONTH(utimestamp), name
order by utimestamp asc

关于mysql - 在 MySQL 中操作字段的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com