目前我有一個名為 test 的表:
| ID | 測驗日期 | 結果 |
|---|---|---|
| 1 | 2021-11-25 | 積極的 |
| 2 | 2021-11-25 | 積極的 |
| 3 | 2021-11-25 | 積極的 |
| 4 | 2021-11-26 | 消極的 |
| 5 | 2021-11-26 | 積極的 |
與 2021 年 11 月 25 日相比,我如何才能在 2021 年 11 月 26 日增加新的陽性病例。結果應顯示一個指示增量的數字。如果新的陽性病例比昨天少,這個數字應該是負數。
預期成績:
| 增量 |
|---|
| -2 |
解釋:1 - 3 = -2
uj5u.com熱心網友回復:
使用條件聚合。
SELECT SUM(test_date = '2021-11-26') - SUM(test_date = '2021-11-25') AS increment
FROM table
WHERE test_date IN ('2021-11-25', '2021-11-26')
AND result = 'positive'
uj5u.com熱心網友回復:
您可以使用下面的動態查詢,您不需要指定日期,您可以簡單地使用 current_date() 和 1 的 current date() 和 date_diff :
select(
select count(result) from test where result = 'positive' and test_date = current_date())
-
(select count(result) from test where result = 'positive' and test_date =DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)) as increment
結果:
increment
-2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/367612.html
標籤:mysql
上一篇:找出報告生成名稱
