1

这是 sql 语句只给出一个值

SELECT count(pn1) FROM pledges
WHERE date1
BETWEEN '2011-05-05' AND '2011-06-06'

我想对同一列或同一表中的不同列多次使用“之间”

例如,我想在 date1 中多次使用它,或者我想将它用于具有不同范围的 date1 和 date2

注意:我正在使用 java DB Derpy(JDBC-Derby)

4

1 回答 1

6

同一列不止一次:

SELECT count(pn1) 
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
       OR date1 BETWEEN '2011-07-05' AND '2011-08-06'

两个不同的列:

SELECT count(pn1) 
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
      AND date2 BETWEEN '2011-05-05' AND '2011-06-06'

编辑:根据评论,也许您正在寻找类似的东西?

SELECT SUM(CASE WHEN date1 BETWEEN '2011-05-05' AND '2011-06-06' THEN 1 ELSE 0) END AS Count1,
       SUM(CASE WHEN date1 BETWEEN '2011-07-05' AND '2011-08-06' THEN 1 ELSE 0) END AS Count2
    FROM pledges
    WHERE date1 BETWEEN '2011-05-05' AND '2011-06-06'
       OR date1 BETWEEN '2011-07-05' AND '2011-08-06'
于 2011-05-06T21:25:27.903 回答