例如,我在表中有我的應用程式配置值。
id appid keyValue dataValue
1 app1 hitcount 3
2 app1 lasthit 2022-03-23 13:15:56

我試圖在 MySQL 中撰寫一個查詢來重置 hitcount = 0,如果 lasthit 日期更改例如如果 23 更改為 24 我想重置 0 hitcount 我寫了這個查詢但得到錯誤 [錯誤代碼:1093。 ]。
update appconfig
SET dataValue = IF ( (select day(dataValue)
from appconfig
where appid = 'app1'
and keyValue = 'lasthit'
) > day(utc_timestamp()),0,dataValue
)
where appid = 'app1'
and keyValue = 'hitcount'
請指教
uj5u.com熱心網友回復:
您可以使用 INNER JOIN
但是這樣的查詢屬于EVENT HANDLER
CREATE TABLE appconfig ( `id` INTEGER, `appid` VARCHAR(4), `keyValue` VARCHAR(8), `dataValue` VARCHAR(10) ); INSERT INTO appconfig (`id`, `appid`, `keyValue`, `dataValue`) VALUES ('1', 'app1', 'hitcount', '3'), ('2', 'app1', 'lasthit', '2022-03-23'), ('3', 'app2', 'hitcount', '3'), ('4', 'app2', 'lasthit', '2022-03-22');
UPDATE appconfig a1 INNER JOIN (SELECT `appid` FROM appconfig WHERe DATE(`dataValue`) < current_date() AND `keyValue` = 'lasthit' ) a ON a.`appid` = a1.`appid` SET dataValue = 0 WHERE keyValue = 'hitcount'
SELECT * FROM appconfig編號 | 應用程式 | 鍵值 | 資料值 -: | :---- | :------- | :--------- 1 | 應用程式1 | 命中數 | 3 2 | 應用程式1 | 最后一擊 | 2022-03-23 3 | 應用程式2 | 命中數 | 0 4 | 應用程式2 | 最后一擊 | 2022-03-22
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/448572.html
上一篇:我可以在一個專案中使用Redux和MongoDB嗎?
下一篇:從正態分布生成亂數
