我有一個名為“testing_tables”的表,假設當前日期是 2022-03-01,1 個月 = 30 天。
| ip | 成立 | PID |
|---|---|---|
| 192.168.1.1 | 2022-01-1 | 1111 |
| 192.168.1.2 | 2022-01-1 | 2222 |
| 192.168.1.3 | 2022-01-1 | 3333 |
| 192.168.1.1 | 2022-02-1 | 1111 |
| 192.168.1.2 | 2022-02-1 | 2222 |
| 192.168.1.3 | 2022-02-1 | 3333 |
| 192.168.1.1 | 2022-03-1 | 1111 |
| 192.168.1.2 | 2022-03-1 | 2222 |
| 192.168.1.4 | 2022-03-1 | 4444 |
我正在努力實作以下結果:
| ip | 成立 | PID | 老化天數 |
|---|---|---|---|
| 192.168.1.1 | 2022-01-1 | 1111 | 90 天 |
| 192.168.1.2 | 2022-01-1 | 2222 | 90 天 |
| 192.168.1.3 | 2022-01-1 | 3333 | 60 天 |
| 192.168.1.4 | 2022-03-1 | 4444 | 0 天 |
基本上,我想嘗試計算第一個 IP 成立的天數,然后自動計算天數。然后在接下來的老化天數中僅顯示 1 個 IP 和 pid。mysql版本:8.0
uj5u.com熱心網友回復:
select ip, min(founded) as founded, pid, DATEDIFF(CURRENT_DATE,min(founded)) as aging_days
from testing_tables
group by pid
order by pid;
uj5u.com熱心網友回復:
嘗試這個
SELECT
ip, MIN(founded) founded, pid, DATEDIFF(CURRENT_TIMESTAMP(), MIN(founded)) aging_days
FROM testing_tables
GROUP BY ip, pid
uj5u.com熱心網友回復:
您可以嘗試以下方法:
SELECT ip, MIN(founded) AS founded, pid, DATEDIFF(CURRENT_TIMESTAMP(), MIN(founded)) AS aging_days FROM testing_tables GROUP BY ip, pid ORDER BY ip
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448891.html
