我想要一个条件来获取日期之间的值。
我有一个表名用户和一个列日期,我想使用日期条件从数据库中获取值。
select sum(visitor) AS V FROM user WHERE date < '21-05-2012' AND dDateTime > '11-05-2012'
select sum(visitor) AS c FROM user WHERE date < '21-06-2012' AND dDateTime > '11-06-2012'
select sum(visitor) AS s FROM user WHERE date < '21-07-2012' AND dDateTime > '11-07-2012'
select sum(visitor) AS g FROM user WHERE date < '21-08-2012' AND dDateTime > '11-08-2012'
我希望在单个sql语句中使用这四个查询
答案 0 :(得分:1)
也许是这样的:
select
sum(CASE WHEN date < '21-05-2012' AND dDateTime > '11-05-2012' THEN visitor ELSE 0 END) AS V,
sum(CASE WHEN date < '21-06-2012' AND dDateTime > '11-06-2012' THEN visitor ELSE 0 END) AS c,
sum(CASE WHEN date < '21-07-2012' AND dDateTime > '11-07-2012' THEN visitor ELSE 0 END) AS s,
sum(CASE WHEN date < '21-08-2012' AND dDateTime > '11-08-2012' THEN visitor ELSE 0 END) AS g
FROM
user