[英]mySQL Update column checking other field value
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'
請指教
您可以使用 INNER JOIN
但是這樣的查詢屬於事件處理程序
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<> 在這里擺弄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.