通过WHERE子句选择所有行

时间:2013-10-27 19:09:31

标签: mysql sql join

我有以下查询可行(没有“WHERE stats.dt”部分)。我向所有用户提供他们的数据。

我的问题是,此查询当然会导致只有具有stats.dt>的用户的行。 $ timestampnow- $ maxdays_data)。但我需要所有用户,但只有当stats.dt大于tstamp-maxdays时才需要获取SUM(上传)或SUM(下载)的值。其中包含stats.dt小于我需要的上传和下载值的行可以忽略。

一个例子是没有选择具有nodeid 2的用户,因为他的dt太小了。我确实希望选择用户,但不想使用数据或上传值(它们可以是0)。

统计数据表如下所示

nodeid |    dt      | upload | download
----------------------------------------
1      | 1381699533 | 345345 | 42324234
1      | 1382899152 | 7575   | 574234
1      | 1380699533 | 764534 | 7235232
2      | 1372899152 | 71455  | 124123

我不知道从哪里开始寻找如何解决这个问题,所以也许有人可以指出我正确的方向。谢谢!

SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
        FROM tariffs
        LEFT JOIN assignments ON tariffs.id = assignments.tariffid
        RIGHT JOIN customers ON assignments.customerid = customers.id
        GROUP BY id
) a
JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, customers.cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, nodes.warning, nodes.access
        FROM customers
        LEFT JOIN nodes ON customers.id = nodes.ownerid
        LEFT JOIN stats ON nodes.id = stats.nodeid
        LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
        WHERE stats.dt > ($timestampnow-$maxdays_data) 
        GROUP BY id
) b ON a.id = b.id
JOIN (
SELECT customerid, SUM(cash.value) AS balance
        FROM cash
        GROUP BY customerid
) c ON b.id = c.customerid

1 个答案:

答案 0 :(得分:0)

这是一种蛮力的方式。它几乎可以肯定地被简化,但是在不了解桌子和外键结构的情况下很难确定。

我所做的就是将sum(stats.download)替换为sum(case when stats.dt > ($timestampnow-$maxdays_data) then s.download end),同样替换上传。我还将b上的连接更改为外连接:

Select
    b.id, 
    b.lastname, 
    b.name, 
    c.balance, 
    a.maxdebt, 
    b.warndata, 
    b.warndownload, 
    b.warnupload, 
    b.warndebt, 
    b.cutoffdata, 
    b.cutoffdownload, 
    b.cutoffupload, 
    b.cutoffdebt, 
    b.data, 
    b.download, 
    b.upload, 
    b.warning, 
    b.access, 
    b.cutoffstop
From (
    Select
        c.id,
        sum(t.value) as maxdebt
    From
        tariffs t
            left join
        assignments a
            on t.id = a.tariffid
            right join 
        customers
            on a.customerid = c.id
        Group by
            c.id
    ) a left outer join (
    Select
        c.id,
        upper(lastname) as lastname,
        c.name,
        sum(s.upload + s.download) as data,
        sum(case when stats.dt > ($timestampnow-$maxdays_data) then s.download end) as download,
        sum(case when stats.dt > ($timestampnow-$maxdays_data) then s.upload end) as upload, 
        c.cutoffstop,
        warndata, 
        warndownload, 
        warnupload,
        warndebt, 
        cutoffdata, 
        cutoffdownload, 
        cutoffupload, 
        cutoffdebt,
        n.warning,
        n.access
    From
        customers c
            left join
        nodes n
            on c.id = n.ownerid
            left join
        stats s
            on n.id = s.nodeid
            left join
        customerwarnings w
            on c.id = w.id
    Group By 
        c.id
    ) b 
        On a.id = b.id
        inner join (
    Select
        customerid, 
        sum(cash.value) as balance
    From
        cash
    Group By
        customerid
    ) c 
    on a.id = c.customerid