SQL查询 - 如何排除特定字段的WHERE

时间:2011-07-29 09:07:46

标签: mysql sql database

请参阅下面的SQL查询,它工作正常。它计算是,否,其他的数量和匹配的手机号码[销售字段](D.MobileNo = S.mobile

SELECT D.Username, 
      SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes, 
      SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not, 
      SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other, 
      SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales, 
      COUNT(*) as TOTAL 
FROM dairy as D 
     LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile 
WHERE source = 'Network' 
      AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 
      AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 
GROUP BY D.Username 
ORDER BY TOTAL DESC

我想排除WHERE字段的Sales - 它不应该是CheckDate的一部分。这意味着它应检查dairy表中没有CheckDate字段的任何记录。

怎么办呢?

2 个答案:

答案 0 :(得分:0)

如果你真的只想在一个查询中获得这些结果,那么这可能就行了。

SELECT D.Username, 
    SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes, 
    SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not, 
    SUM(CASE WHEN D.type = ''    AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other, 
    SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales, 
    COUNT(*) as TOTAL,
    SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE
FROM dairy as D 
   LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile 
WHERE source = 'Network' 
GROUP BY D.Username 
ORDER BY TOTAL DESC

请注意,“TOTAL”将计算所有行(包括那些不在CheckDate范围内的行),TOTALINCHECKDATE返回与上一个查询中相同的值。

显然,这仍然可以优化。

答案 1 :(得分:0)

假设SALES表中也存在用户名

SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not`
  , SUM(Other) As Other, SUM(sales) Sales, SUM(total)
FROM (
-- get diary data
SELECT username,mobileNo As mobile, 
      CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes, 
      CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`, 
      CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other, 
      0 As sales, 1 as total
FROM dairy as D 
WHERE source = 'Network' 
      AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 
      AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 

UNION ALL
-- get all sales
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total
FROM sales
  WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 
      AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 
) AS a
GROUP BY Username

还可以尝试添加日期的原始查询

SELECT D.Username, 
      SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes, 
      SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not, 
      SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other, 
      SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales, 
      COUNT(*) as TOTAL 
FROM dairy as D 
     LEFT JOIN (SELECT DISTINCT mobile FROM sales WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 ) as S on D.MobileNo = S.mobile 
WHERE source = 'Network' 
      AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 
      AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 
GROUP BY D.Username 
ORDER BY TOTAL DESC