0

我想要一个条件来获取日期之间的值。

我有一个表名用户和一个列日期,我想使用日期条件从数据库中获取值。

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 语句中

4

1 回答 1

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  
于 2012-06-05T07:23:41.813 回答