SELECT `g`.`name`, `u`.`name`, `u`.`score`
FROM (
SELECT `ug`.`group_id` AS `group_id`, MAX(`u`.`score`) AS `score`
FROM `user_to_groups` AS `ug`
JOIN `users` AS `u` ON `u`.`id` = `ug`.`user_id`
GROUP BY `ug`.`group_id`
) AS `m`
JOIN `user_to_groups` AS `ug` ON `ug`.`group_id` = `m`.`group_id`
JOIN `users` AS `u` ON `u`.`id` = `ug`.`user_id` AND `u`.`score` = `m`.`score`
JOIN `groups` AS `g` ON `g`.`id` = `m`.`group_id`
SELECT
(SELECT COUNT( `State`) FROM `table3`
WHERE `State` = '0') AS `count_State`,
(SELECT COUNT( `State_J`) FROM `table3`
WHERE `State_J` = '0') AS `count_State_J`,
(SELECT COUNT( `ssh_state`) FROM `table3`
WHERE `ssh_state` = '0') AS `count_ssh_state`,
(SELECT COUNT( `ssh_state_J`) FROM `table3`
WHERE `ssh_state_J` = '0') AS `count_ssh_state_J`,
(SELECT COUNT( * ) FROM `table3`) AS `count`
SELECT `t`.*, IFNULL(`o`.`count`, 0) AS count_o
FROM `tasks` AS `t`
LEFT JOIN (
SELECT `task_id`, COUNT(*) AS `count`
FROM `offers`
WHERE `user_id` <> 2 AND `status` NOT IN (1,2)
GROUP BY `task_id`
) AS `o` ON `o`.`task_id` = `t`.`id`
WHERE `t`.`city_id` = 1 AND `t`.`status` = 1
typedef enum {
red,
yellow,
green
} color;
color traffic_light_state;
traffic_light_state = red;
typedef enum {
red = -1,
yellow,
green = 3
} color;