简体   繁体   English

IF语句在SQL中的where子句中

[英]IF statement inside where clause in SQL

I'm developing a calendar app. 我正在开发一个日历应用程序。 It stores times in two formats. 它以两种格式存储时间。 The table have following columns title, startDate, startTime . 该表具有以下列title, startDate, startTime If the user provides start time plus start date. 如果用户提供开始时间加上开始日期。 The database stores UNIX time stamp(number of seconds since UNIX epok) in the column startTime , while startDate is NULL . 数据库在startTime列中存储UNIX时间戳(自UNIX epok以来的秒数),而startDate为NULL If the user only provide a start date, the database stores the date in format nnnn-mm-dd in startDate and NULL in ´startTime`. 如果用户仅提供开始日期,则数据库将在startDate中以nnnn-mm-dd格式存储日期,在'startTime`中存储NULL I have this DB structure, because it's easier to display times around the world, because different timezones have different daylight saving times. 我有这种数据库结构,因为它更容易在世界各地显示时间,因为不同的时区有不同的夏令时。

I want select the events that are occurring after a specified date. 我想选择在指定日期之后发生的事件。 The client computer provide the server with a UNIX timestamp of the beginning of that day( $unix ) and a date( $date ) in the format nnnn-mm-dd to select the correct dates. 客户端计算机为服务器提供当天开始的UNIX时间戳( $unix )和格式为nnnn-mm-dd的日期( $date )以选择正确的日期。

The problem is, I don't know how to select those days that are occurring as specified above. 问题是,我不知道如何选择上面指定的那些日子。 This solution is not applicable for me, even though it works: 此解决方案不适用于我,即使它有效:

SELECT *
  FROM events
 WHERE startDate >= '$date'
   OR startTime >= '$unix'

The thing is I have in some rows where unix time stamp is provided in startTime , I also have a date provided in startDate and other reason I which I don't want to explain. 事情是我在某些行中在startTime提供了unix时间戳,我也有一个在startDate提供的日期以及我不想解释的其他原因。 And because of that I can't use the solution above. 因此,我不能使用上面的解决方案。

I need some kind of solution that have an IF statement inside the Where clause like: 我需要某种在Where子句中包含IF语句的解决方案,如:

SELECT *
  FROM events
 WHERE IF(startTime = NULL, startDate >= '$date', startTime >= '$unix')

I'm just guessing this solution. 我只是猜测这个解决方案。 But is it right? 但这是对的吗?

WHERE (startTime IS NULL AND startDate >= '$date')
   OR (startTime IS NOT NULL AND startTime >= '$unix')

All SQL dialects support CASE WHEN: 所有SQL方言都支持CASE WHEN:

SELECT *
 FROM events
WHERE CASE WHEN startTime is null
           THEN startDate >= '$date'
           ELSE startTime >= '$unix'

I know this is an old question but I think this is the best way to proceed... 我知道这是一个老问题,但我认为这是继续进行的最佳方式......

SELECT *
FROM events
WHERE IF(startTime IS NULL, '$date', '$unix') =< startDate

You just need some combined boolean logic in the WHERE clause: 你只需要WHERE子句中的一些组合布尔逻辑:

SELECT * 
FROM events
WHERE 
    (startDate IS NULL AND startTime >= '$unix') OR
    (startTime IS NULL AND startDate >= '$date')
SELECT * 
FROM events 
WHERE 
  (startDate >= '$date' OR startTime >= '$unix')
AND 
  startDate != NULL

This will not return any row that has null value for startDate 这不会返回任何具有startDate值为null的行

I'm assuming that you want to use startDate when startDate is not null, and startTime when startTime is not null... then try this... 我假设你想在startDate不为null时使用startDate,而在startTime不为null时使用startTime ...然后尝试这个...

SELECT * 
FROM events
WHERE (startTime is not NULL and startDate >= '$date') 
OR (startTime is not NULL and startTime >= '$unix')

You can use the CASE statement also, but this makes more sense is more readable. 您也可以使用CASE语句,但这更有意义,更具可读性。

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

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