Considering this small sample set of data:
| 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
I want to get results grouped by territory_id
where the entire group does not contain a row where signed_in
is NULL
. Or basically I want to get these results:
| 5 | 3 | 2013-03-06 01:00:00
| 6 | 3 | 2013-11-20 01:00:00
This is my current sql which looks for the max signed_in
value of each group while being joined to the territories
table:
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
try this one:
select * from territories
where territory_id not in (
select territory_id from territories where signed_in is null);
Here is a query that should return the result you're looking for:
SELECT *
FROM territories T
LEFT OUTER JOIN (SELECT DISTINCT T2.territory_id
FROM territories T2
WHERE T2.signed_in IS NULL) TN ON TN.territory_id = T.territory_id
WHERE TN.territory_id IS NULL
Hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.