gpt4 book ai didi

mysql - 如何使用子选择更新表

转载 作者:行者123 更新时间:2023-11-29 13:10:00 26 4
gpt4 key购买 nike

我正在尝试将子选择的输出与更新表中的字段连接起来。但使用 MySQL 却无法得到正确的结果。

知道如何正确执行吗?

update INSTRUMENTDAILYINFO info 
set
info.vWap = (SELECT
t1.vWap
FROM
INSTRUMENTDAY t1
JOIN
(SELECT
Date(tradedate) date_date,
stockName,
min(tradedate) min_date
FROM
INSTRUMENTDAY
GROUP BY date_date , stockName) t2 ON t1.tradedate = t2.min_date
AND t1.stockName = t2.stockName
order by t1.tradedate , t1.stockName asc)
where
t1.tradeDate = info.tradeDate
and t1.stockName = info.stockName

SELECT
`INSTRUMENTDAILYINFO`.`ID`,
`INSTRUMENTDAILYINFO`.`CLOSEPRICE`,
`INSTRUMENTDAILYINFO`.`HIGHVALUE`,
`INSTRUMENTDAILYINFO`.`LOWVALUE`,
`INSTRUMENTDAILYINFO`.`STARTPRICE`,
`INSTRUMENTDAILYINFO`.`STOCKNAME`,
`INSTRUMENTDAILYINFO`.`TRADEDATE`
FROM `INSTRUMENTDAILYINFO`;

SELECT
`INSTRUMENTDAY`.`ID`,
`INSTRUMENTDAY`.`STOCKNAME`,
`INSTRUMENTDAY`.`TRADEDATE`,
`INSTRUMENTDAY`.`VWAP`
FROM `SimpleGrowth`.`INSTRUMENTDAY`;

最佳答案

UPDATE INSTRUMENTDAILYINFO info 
JOIN
(SELECT t1.vWap,t1.tradedate as tradedate,t2.stockName as stockName
FROM INSTRUMENTDAY t1
JOIN
(SELECT
Date(tradedate) date_date,
stockName,
min(tradedate) min_date
FROM
INSTRUMENTDAY
GROUP BY date_date , stockName) t2 ON t1.tradedate = t2.min_date
AND t1.stockName = t2.stockName
ORDER BY t1.tradedate , t1.stockName asc)x
ON
x.tradeDate = info.tradeDate
AND x.stockName = info.stockName
SET info.vWap= x.vWap

如果主表和 x 子查询之间有连接列,则类似这样。

关于mysql - 如何使用子选择更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219854/

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