[英]Table name specified twice, both as a target for update and as a separate source for data
I am receiving an expected error from my code whenever I attempt to update my table using a select statement although I am trying different workarounds with no hopes to success.每当我尝试使用 select 语句更新我的表时,我都会从我的代码中收到预期错误,尽管我正在尝试不同的解决方法但没有成功的希望。 Any help with my issue would be highly appreciated.对我的问题的任何帮助将不胜感激。 Thank you.谢谢你。
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
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
I finally got it working as following:我终于让它工作如下:
UPDATE `table`
SET Slope = CASE
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 'Uptrend'
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 'Downtrend'
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 'Notrend'
else 'unknown' END
If you just want a query to calculate the slope, something like this should work:如果您只想查询来计算斜率,则应该使用以下方法:
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
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.