0
发现考虑到数据的这个小样本集:Mysql的排除组,如果NULL值组
| id | territory_id | signed_in
| 1 | 6 | 2010-12-22 01:00:00
| 2 | 6 | 2011-12-11 01:00:00
| 3 | 6 | 2013-03-13 01:00:00
| 4 | 6 | NULL
| 5 | 3 | 2013-03-06 01:00:00
| 6 | 3 | 2013-11-20 01:00:00
我想territory_id
获得分组结果,其中整组不包含一排,其中signed_in
是NULL
。或者基本上我想要得到这些结果:
| 5 | 3 | 2013-03-06 01:00:00
| 6 | 3 | 2013-11-20 01:00:00
这是我当前的SQL同时被加入到territories
表看起来每个组的最大signed_in
值:
SELECT `territories`.`id`, `territories`.`label`, `territories`.`type_id`, `territories`.`area_type_id`, `territories`.`map_embed_id`, `tsio`.`signed_in`
FROM `territories` INNER JOIN (
SELECT territory_id, MAX(signed_in) signed_in
FROM `territories_sign_in_out`
GROUP BY territory_id) tsio ON `territories`.`id` = `tsio`.`territory_id`
WHERE `territories`.`type_id` = ?
ORDER BY `tsio`.`signed_in` ASC
LIMIT 15
*基本上我想得到这些结果:*,so where territory_id = 3 ??? – 2014-08-30 04:56:30
这只是一个小数据示例,我不能只选择特定的id,而是需要所有结果减去在'signed_in'上包含NULL值的任何组。更有意义? – 2014-08-30 04:58:21
尝试子查询'WHERE territory_id NOT IN(SELECT DISTINCT territory_id FROM territories_sign_in_out WHERE signed_in IS NULL)' – bansi 2014-08-30 05:10:12