1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| "10001"|"测试"|"54"|"2020-01-01"|"9999-12-31" "10002"|"运行"|"90"|"2020-01-01"|"9999-12-31" "10003"|"测试"|"10"|"2020-01-01"|"9999-12-31"
"10001"|"运行"|"90"|"2020-01-02" "10008"|"测试"|"70"|"2020-01-02"
SELECT uid,state,VALUE,start_time,end_time FROM ( SELECT a.uid,a.state,a.value, a.start_time, CASE WHEN a.end_time = '9999-12-31' AND today.uid IS NOT NULL THEN '2020-01-01' ELSE a.end_time END AS end_time FROM ( SELECT * FROM test_his WHERE start_time <= '2020-01-01' AND end_time >= '2020-01-01' ) a LEFT JOIN ( SELECT * FROM test_update ) today ON a.uid=today.uid UNION SELECT uid,state,VALUE, '2020-01-02' AS start_time, '9999-12-31' AS end_time FROM test_update ) his;
SELECT * FROM test_his WHERE start_time <= '2020-01-01' AND end_time >= '2020-01-01';
SELECT * FROM test_his WHERE end_time < '2020-01-02' UNION ALL SELECT uid,state,VALUE,start_time,'9999-12-31' AS end_time FROM test_his WHERE end_time = '2020-01-02' UNION ALL SELECT uid,state,VALUE,start_time,'9999-12-31' AS end_time FROM test_his WHERE start_time < '2020-01-03' AND end_time >= '2020-01-03';
SELECT uid,state,VALUE,start_time,end_time FROM ( SELECT a.uid,a.state,a.value, a.start_time, IF(today.uid IS NULL,'9999-12-31','2020-01-01') AS end_time FROM ( SELECT * FROM test_his WHERE start_time <= '2020-01-01' AND end_time >= '2020-01-01' ) a LEFT JOIN ( SELECT * FROM test_update WHERE dt='2020-01-02' ) today ON a.uid=today.uid UNION SELECT uid,state,VALUE, '2020-01-02' AS start_time, '9999-12-31' AS end_time FROM test_update WHERE dt='2020-01-02' ) his;
|