sql如果date等于今天而不是添加AND

时间:2015-03-05 22:34:01

标签: php mysql sql

SELECT e.event_name, e.event_start_date, e.event_start_time, e.post_content
        FROM {$wpdb->prefix}em_events e
        WHERE e.event_status = '1'
        AND e.event_start_date >= '{$date_sever}'
        AND e.event_start_time >= '{$time_sever}'
        ORDER BY e.event_start_date ASC

我想仅在AND e.event_start_time >= '{$time_sever}'

时才会运行此行AND e.event_start_date = '{$date_sever}'

任何想法如何?

2 个答案:

答案 0 :(得分:2)

试试

SELECT e.event_name, e.event_start_date, e.event_start_time, e.post_content
    FROM {$wpdb->prefix}em_events e
    WHERE e.event_status = '1'
    AND e.event_start_date >= '{$date_sever}' 
    AND (e.event_start_time >= '{$time_sever}' 
        OR e.event_start_date > '{$date_sever}') 
    ORDER BY e.event_start_date ASC

答案 1 :(得分:1)

使用OR运算测试两个条件。

    SELECT e.event_name, e.event_start_date, e.event_start_time, e.post_content
        FROM {$wpdb->prefix}em_events e
    WHERE 
        e.event_status = '1' 
      AND
        e.event_start_date > '{$date_sever}'
      OR 
        (e.event_start_date = '{$date_sever}' AND e.event_start_time >= '{$time_sever}')
    ORDER BY e.event_start_date ASC

它将测试第一个表达式是否比{$date_sever}更强大。如果它不是GREATHER,将调用OR来测试表达式是否为EQUAL {$date_sever}并且将评估第二个表达式。