如何在一个sql语句中对同一列或不同列使用'BETWEEN'条件两次

时间:2011-05-06 21:21:17

标签: sql jdbc derby between javadb

这是sql语句只给出一个值

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

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

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

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

1 个答案:

答案 0 :(得分: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'