0

我有一个相当简单的查询:

SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id = 'YYYY'
    AND [date] < '2012-06-27';

我想知道是否有一种方法可以根据条件切换列 [日期],在这种情况下是较小或最早的。

换句话说,像

SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id = 'YYYY'
    AND (IF(time_created <= transaction_date)) time_created < '2012-06-27' ELSE 
        transaction_date > '2012-06-27');

因此,通过比较两列来使用行中最早日期的查询(尽管似乎 time_created 应该总是最早的,但这里不是这种情况)。如果没有,我可以将查询分成两部分,但我想知道它是否可以合二为一。

4

2 回答 2

3
SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id = 'YYYY'
AND IF(time_created <= transaction_date, time_created < '2012-06-27', transaction_date > '2012-06-27');

或 ANSI SQL 解决方案:

SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id = 'YYYY'
AND CASE WHEN time_created <= transaction_date
         THEN time_created < '2012-06-27'
         ELSE transaction_date > '2012-06-27'
    END
于 2012-06-27T22:00:52.570 回答
3

您可以使用双重条件,例如:

and  (
    time_created <= transaction_date and time_created < '2012-06-27' or
    time_created > transaction_date and transaction_date > '2012-06-27'
)
于 2012-06-27T22:01:07.107 回答