SELECT
cms_module_ladu_batch.id AS batch_id,
name, t1, cms_module_ladu_batch.date_changed AS date_ch,
t13, t2, t3, t4, bkuup, date_modified, date_created,
kogused.aadress AS aadress,
kogused.kogus AS kogus,
SUM(kogused.kogus) AS total
FROM
cms_module_ladu_batch
LEFT JOIN kogused
ON cms_module_ladu_batch.id = kogused.bid
WHERE
t2!='Sildid'
GROUP BY
batch_id
这就是MySQL代码 - 问题如下。我需要排除total为0且date_ch早于一个月的行(date_cd格式为“2011-01-01 00:00:00”)。 可能吗? http://jsfiddle.net/tiitremmel/2mvrr/1/ - jsfiddle示例 - 第一个表就像mysql结果是默认的,第二个表是结果的样子。
答案 0 :(得分:2)
将日期条件添加到WHERE
- 语句中,并将SUM
排除在外
HAVING
- 声明。它应该是这样的:
SELECT cms_module_ladu_batch.id AS batch_id,
cms_module_ladu_batch.date_changed AS date_ch,
kogused.kogus AS kogus,
SUM(kogused.kogus) AS total
FROM cms_module_ladu_batch
LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid
WHERE t2 != 'Sildid'
AND (
(
cms_module_ladu_batch.date_changed < DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND SUM(kogused.kogus) > 0
)
OR
cms_module_ladu_batch.date_changed > DATE_SUB(NOW(), INTERVAL 1 MONTH)
)
GROUP BY batch_id;
或者......使用UNION-Statement将两个SELECTS粘合在一起。
SELECT cms_module_ladu_batch.id AS batch_id,
cms_module_ladu_batch.date_changed AS date_ch,
kogused.kogus AS kogus,
SUM(kogused.kogus) AS total
FROM cms_module_ladu_batch
LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid
WHERE t2 != 'Sildid'
AND cms_module_ladu_batch.date_changed < DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY batch_id
HAVING total > 0
UNION
SELECT cms_module_ladu_batch.id AS batch_id,
cms_module_ladu_batch.date_changed AS date_ch,
kogused.kogus AS kogus,
SUM(kogused.kogus) AS total
FROM cms_module_ladu_batch
LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid
WHERE t2 != 'Sildid'
AND cms_module_ladu_batch.date_changed > DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY batch_id;
尝试两者,看看最适合您的解决方案。
答案 1 :(得分:1)
SELECT
cms_module_ladu_batch.id AS batch_id,
name, t1, cms_module_ladu_batch.date_changed AS date_ch,
t13, t2, t3, t4, bkuup, date_modified, date_created,
kogused.aadress AS aadress,
kogused.kogus AS kogus,
SUM(kogused.kogus) AS total
FROM
cms_module_ladu_batch
LEFT JOIN kogused
ON cms_module_ladu_batch.id = kogused.bid
WHERE
t2!='Sildid' AND
date_ch < date_sub(now(), interval 1 month)
GROUP BY
batch_id
HAVING
SUM(kogused.kogus) <> 0
答案 2 :(得分:0)
要在GROUP BY子句之外进行过滤,您可以使用HAVING关键字。
答案 3 :(得分:0)
只需在底部添加:
HAVING (total<>0) AND (date_cd>=ADDDATE(CURDATE(), INTERVAL -1 MONTH))
其他回复(见评论):
HAVING (total<>0) OR (date_cd>=ADDDATE(CURDATE(), INTERVAL -1 MONTH))
答案 4 :(得分:0)
SELECT cms_module_ladu_batch.id AS batch_id,
cms_module_ladu_batch.date_changed AS date_ch,
kogused.kogus AS kogus,
SUM(kogused.kogus) AS total
FROM cms_module_ladu_batch
LEFT JOIN kogused ON cms_module_ladu_batch.id = kogused.bid
WHERE t2 != 'Sildid'
GROUP BY batch_id
HAVING (
(
cms_module_ladu_batch.date_changed < DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND SUM(kogused.kogus) > 0
)
OR
cms_module_ladu_batch.date_changed > DATE_SUB(NOW(), INTERVAL 1 MONTH)
);
这是最适合我的代码。非常感谢所有人,但对Bjoern最重要 - 你对代码的想法对我帮助很大!