2017-04-24 138 views

回答

3

見WHERE子句中下面的例子

#standardSQL 
WITH yourTable AS (
    SELECT i, date 
    FROM UNNEST(GENERATE_DATE_ARRAY('2017-04-15', '2017-04-28')) AS date WITH OFFSET AS i 
) 
SELECT * 
FROM yourTable 
WHERE date <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY) 
-- ORDER BY date 

順便說一下,萬一如果你還在用傳統的SQL - 見例如下面

#legacySQL 
SELECT * 
FROM -- yourTable 
    (SELECT 1 AS id, DATE('2017-04-20') AS date), 
    (SELECT 2 AS id, DATE('2017-04-21') AS date), 
    (SELECT 3 AS id, DATE('2017-04-22') AS date), 
    (SELECT 4 AS id, DATE('2017-04-23') AS date), 
    (SELECT 5 AS id, DATE('2017-04-24') AS date), 
    (SELECT 6 AS id, DATE('2017-04-25') AS date) 
WHERE TIMESTAMP(date) <= DATE_ADD(TIMESTAMP(CURRENT_DATE()), -3, 'DAY') 
-- ORDER BY date 
1

這適用於字符串格式化的日期。

DATE(TIMESTAMP(date)) <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)

相關問題