SQL在where子句中使用日期

时间:2012-06-22 14:56:54

标签: sql inner-join where

我这里有这个SQL代码..

SELECT cl.clientid,
       cl.clientname,
       cl.billingdate,
       cp.startdate,
       cp.expiration, 
       (SELECT COUNT(*)
            FROM invoice 
            HERE client = cl.clientid) AS invoicecount  
FROM client cl
INNER JOIN clientplan cp ON cp.client = cl.clientid
WHERE cl.isbilled = 1 AND expiration is NULL AND expiration > '2012-06-22'

我的问题是客户端可能有3个客户端计划,他们要么有到期日期,要么到期为NULL。我试图获取未过期或为NULL的行。

我做错了什么?

1 个答案:

答案 0 :(得分:4)

我不确定,但不是你想要的:

SELECT cl.clientid, cl.clientname, cl.billingdate, cp.startdate, cp.expiration, 
(select count(*) from invoice where client = cl.clientid) as invoicecount  
FROM client cl
inner join clientplan cp on cp.client = cl.clientid
where cl.isbilled = 1 and (expiration is NULL or expiration > '2012-06-22')

使用或代替过期和过期。因为到期不能为空且> '2012-06-22' ...