开发者

how to use 'BETWEEN' condetion two times in one sql statement for same column or diffrent columns

开发者 https://www.devze.com 2023-03-03 05:36 出处:网络
this is sql statement give only one value SELECT count(pn1) FROM pledges WHERE date1 BETWEEN \'2011-05-05\' AND \'2011-06-06\'

this is sql statement give only one value

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

i want use "between" more than one time for same column or for different columns in same table

for example i want use it in date1 more than one开发者_开发问答 time or i want use it for date1 and date2 with different range

NOTE:I'm using java DB Derpy(JDBC-Derby)


More than once for the same column:

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'

Two different columns:

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'

EDIT: Based on comment, perhaps you're looking for something like this instead?

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号