[英]Need an alternative for aggregate function in mySQL
我在 MySQL 中遇到了一些聚合函數的問題,我幾乎已經在到處尋找它,但沒有答案。
我想編寫一個查詢來查找 LOWEST SEQUENCE AND REQUEST_PENDING STATUS 然后在某個條件后更新它。
例如:
下邊是,
column_person_name | 列狀態 | 列序列 |
---|---|---|
一個 | request_progressed | 1 |
b | request_pending | 2 |
c | request_pending | 3 |
如果名為 A 的人完成,它的狀態將從 request_progressed 更改為 request_done,然后下一個人 B 將其狀態更改為 request_progressed 而人 C 保持不變,因為它的序列是第三個
注意:我只需要知道如何通過找到具有 request_pending 狀態的最低序列來更改人員 B 狀態並且表上只有一個 request_progressed 這就是為什么當它更新為“request_done”時“request_pending”需要更新為“request_progressed”
我嘗試了以下方法:
update table
set
status = 'request_progressed'
WHERE status = 'pending' AND sequence = min(sequence)
在 MySQL 中:
update t
set status = 'request_progressed'
where status = 'request_pending'
order by sequence
limit 1
在 PostgreSQL 中:
update t
set status = 'request_progressed'
where (status, sequence) in (
select status, sequence
from t
where status = 'request_pending'
order by sequence
offset 0 rows fetch first 1 row only
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.