简体   繁体   English

MySQL:在SELECT语句中使用IF条件比较日期

[英]MySQL: Comparing date using IF condition in SELECT statement

Sample data: 样本数据:

db1.locationDetails table db1.locationDetails表

| id | locationUID | locationName |
|----|-------------|--------------|
| 1  | L0001       | Site A       |
| 2  | L0002       | Site B       |
| 3  | L0003       | Site C       |
| 3  | L0004       | Site D       |

db2.HealthData table db2.HealthData表

| id | locationID  | Date_Time        | memUsage |
|----|-------------|------------------|----------|
| 1  | L0001       | 2018-09-10 11:43 |   35     |
| 2  | L0002       | 2018-09-10 08:22 |   39     |
| 3  | L0003       | 2018-09-10 14:44 |   43     |
| 4  | L0004       | 2018-09-10 16:01 |   72     |
| 5  | L0001       | 2018-09-12 01:26 |   50     |
| 6  | L0002       | 2018-09-12 03:15 |   32     |

I have a query: 我有一个查询:

SELECT DISTINCT db1.locationDetails.locationUID,
    db1.locationDetails.locationName,
    MAX(db2.HealthData.Date_Time),
    db2.HealthData.memUsage,
    IF(DATE(db2.HealthData.Date_Time) = '2018-09-12', "ON", "OFF") AS Status
FROM db1.locationDetails
LEFT JOIN db2.HealthData
ON db1.locationDetails.locationUID = db2.HealthData.locationID
GROUP BY db1.locationDetails.locationUID

Based on my understanding, the 'Status' column will show "ON" if the Date is equals to 2018-09-12 but somehow it always returns "OFF" regardless of whether the value in the Date_Time column is equal to the Date value specified in the query. 根据我的理解,如果Date等于2018-09-12,“状态”列将显示“ ON”,但是无论如何Date_Time列中的值是否等于指定的Date值,总会返回“ OFF”在查询中。

Can anyone tell me what is wrong here? 谁能告诉我这是怎么回事? Thanks in advance. 提前致谢。

Expected output: 预期产量:

| locationUID | locationName | Date_Time       | memUsage | Status |
|-------------|--------------|-----------------|----------|--------|
| L0001       | Site A       |2018-09-12 01:26 |   50     | ON     |
| L0002       | Site B       |2018-09-12 03:15 |   32     | ON     |
| L0003       | Site C       |2018-09-10 14:44 |   43     | OFF    |
| L0004       | Site D       |2018-09-10 16:01 |   72     | OFF    |

Use subquery to get your desired result: 使用子查询来获得所需的结果:

   select x.locationuid,x.locationname,maxitme, memusage, case when date(maxtime)='2018-09-12' then 'ON' else 'OFF' end as status
    from db1.locationDetails x
    inner join
    (select a.locationuid,maxtime,memusage
    from 
    (SELECT locationUID,MAX(Date_Time) as maxtime FROM db2.HealthData group by locationUID)a
    inner join db2.HealthData b on a.locationuid=b.locationuid)y
    on x.locationuid=y.locationuid

add Group by db1.locationDetails.locationUID,db2.HealthData.id 通过db1.locationDetails.locationUID,db2.HealthData.id添加组

SELECT DISTINCT db1.locationDetails.locationUID,
        db1.locationDetails.locationName,
        MAX(db2.HealthData.Date_Time),
        db2.HealthData.memUsage,
        IF(DATE(db2.HealthData.Date_Time) = '2018-09-12', "ON", "OFF") AS Status
    FROM db1.locationDetails
    LEFT JOIN db2.HealthData
    ON db1.locationDetails.locationUID = db2.HealthData.locationID
    GROUP BY db1.locationDetails.locationUID,db2.HealthData.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM