简体   繁体   English

表名指定两次,既作为更新的目标,又作为单独的数据源

[英]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.

相关问题 表名称指定两次作为更新目标和单独的数据源 - Table name specified twice both as a target for update and separate source for data MySQL-表两次指定为更新目标和单独数据源 - MySQL - table specified twice both as a target for update and as a separate source for data 表被指定两次,既作为 &#39;UPDATE&#39; 的目标,也作为 mysql 中数据的单独源 - Table is specified twice, both as a target for 'UPDATE' and as a separate source for data in mysql 表被指定两次,既作为 &#39;UPDATE&#39; 的目标,也作为单独的数据源 - Table is specified twice, both as a target for 'UPDATE' and as a separate source for data 表被指定两次,既作为&#39;UPDATE&#39;的目标,又作为单独的数据源 - Table is specified twice, both as a target for 'UPDATE' and as a separate source for data 表&#39;tbl_name&#39;被指定两次,既作为&#39;UPDATE&#39;的目标,又作为数据的单独源 - Table 'tbl_name' is specified twice, both as a target for 'UPDATE' and as a separate source for data 错误: <table_name> 指定两次,既作为“ UPDATE”的目标,又作为单独的数据源 - Error : <table_name> specified twice, both as a target for 'UPDATE' and as a separate source for data 该表被两次指定为INSERT的目标和单独的数据源 - table is specified twice both as a target for INSERT and as separate source of data SQL-#1093-表&#39;&#39;被指定两次,既作为&#39;UPDATE&#39;的目标又作为数据的单独来源 - SQL - #1093 - Table '' is specified twice, both as a target for 'UPDATE' and as a separate source for data java.sql.SQLException:表被指定了两次,既作为“更新”的目标,又作为数据的单独源 - java.sql.SQLException: Table is specified twice, both as a target for 'UPDATE' and as a separate source for data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM