每当我尝试使用select语句更新表时,都会从代码中收到预期的错误,尽管我正在尝试不同的解决方法,但没有成功的希望。我的问题的任何帮助将不胜感激。谢谢。
UPDATE `table`
SET Slope = CASE
when (SELECT ((SELECT (SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1) FROM DUAL)) as x) > 0 THEN 'Uptrend'
when (SELECT ((SELECT (SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1) FROM DUAL)) AS x) < 0 THEN 'Downtrend'
when (SELECT ((SELECT (SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT dialcount FROM `table` ORDER by DateColumn ASC limit 1) FROM DUAL)) AS x) = 0 THEN 'Notrend'
else 'unknown' END
UPDATE `table`
SET Slope = CASE
when ((SELECT (SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1) FROM DUAL)) > 0 THEN 'Uptrend'
when ((SELECT (SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1) FROM DUAL)) < 0 THEN 'Downtrend'
when ((SELECT (SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT dialcount FROM `table` ORDER by DateColumn ASC limit 1) FROM DUAL)) = 0 THEN 'Notrend'
else 'unknown' END
答案 0 :(得分:0)
update table set Slope = case
when ((SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1))>0 then 'Uptrend'
when ((SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1))<0 then 'Downtrend'
when ((SELECT IntColumn FROM `table` ORDER by DateColumn desc limit 1) - (SELECT IntColumn FROM `table` ORDER by DateColumn ASC limit 1))=0 then 'Notrend'
else 'unknown' end
希望有帮助!
答案 1 :(得分:0)
我很高兴在经过大量搜索,尝试和错误之后终于可以为我工作,并且我想与希望与case方法一起使用此示例的任何人分享。它如下:
更新table
SET斜率=箱
when((SELECT(SELECT(SELECT intcolumn FROM(SELECT intcolumn FROM table
ORDER by DateColumn desc limit 1)as lastdc)-(SELECT intcolumn FROM(SELECT intcolumn FROM table
ORDER by DateColumn ASC limit 1) as firstdc)FROM DUAL)as dff))> 0 THEN'上升趋势'
when((SELECT(SELECT(SELECT intcolumn FROM(SELECT intcolumn FROM table
ORDER by DateColumn desc limit 1)as lastdc)-(SELECT intcolumn FROM(SELECT intcolumn FROM table
ORDER by DateColumn ASC limit 1) as firstdc)FROM DUAL)as dff))<0 THEN'下降趋势'
when((SELECT(SELECT(SELECT intcolumn FROM(SELECT intcolumn FROM table
ORDER by DateColumn desc limit 1)as lastdc)-(SELECT intcolumn FROM(SELECT intcolumn FROM table
ORDER by DateColumn ASC limit 1) as firstdc)FROM DUAL)as dff))= 0然后'Notrend'
其他“未知” END
答案 2 :(得分:0)
如果您只想查询计算斜率,则应执行以下操作:
SELECT CASE WHEN lastInt > firstInt THEN 'Uptrend'
WHEN lastInt > firstInt THEN 'Downtrend'
WHEN lastInt > firstInt THEN 'Notrend'
END AS `slope`
FROM (
SELECT (SELECT IntColumn FROM `table` ORDER BY DateColumn DESC LIMIT 1) AS lastInt
, (SELECT IntColumn FROM `table` ORDER BYDateColumn ASC LIMIT 1) AS firstInt
) AS boundInts
;