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
$curdate = '';
while (($row = $result_set -> fetch_assoc()) != false) {
$date = sdate("M Y", 0, $row["date"]);
if ($curdate != $date) {
echo "<h2>Отчетность за ".sdate("M Y", 0, $row["date"])."</h2>";
$curdate = $date;
}
echo "<a href=\"".$row["link"]."\">".$row["name"]."</a>";
echo "<span>(".fdate("d M Y h:i", 0, $row["date"]).")</span><hr>";
}
SELECT `s`.`user`, `u`.`name`, `s`.`OCI`, `s`.`OCO`, `s`.`CCP`, `s`.`PV`, `us`.`total_games`,
`s2`.`total_games` AS `today_games`, `s3`.`total_games` AS `yesterday_games`,
`s4`.`date` AS `high_OCI_date`
FROM `qz_user_stat_date` AS `s`
JOIN `qz_users` AS `u` ON `u`.`id` = `s`.`user`
JOIN `qz_user_stat` AS `us` ON `us`.`user` = `s`.`user`
LEFT JOIN `qz_user_stat_date` AS `s2` ON `s2`.`user` = `s`.`user` AND `s2`.`date` = :today
LEFT JOIN `qz_user_stat_date` AS `s3` ON `s3`.`user` = `s`.`user` AND `s3`.`date` = :yesterday
LEFT JOIN (
SELECT `t`.`user` AS `usr`, min(`t`.`date`) AS `date`
FROM `qz_user_stat_date` AS `t`
JOIN (
SELECT `user`, max(`OCI`) AS `oci`
FROM `qz_user_stat_date`
GROUP BY `user`
) AS `o` ON `o`.`user` = `t`.`user` AND `o`.`oci` = `t`.`OCI`
GROUP BY `t`.`user`
) AS `s4` ON `s4`.`usr` = `s`.`user`
SELECT `post_id` FROM `posts` WHERE `author_id` IN (:id1, :id2, :id3)
SELECT `p1`.`post_id`
FROM `posts` AS `p1`
JOIN `posts` AS `p2`
ON `p2`.`post_id` = `p1`.`post_id` AND `p2`.`author_id` = :id2
JOIN `posts` AS `p3`
ON `p3`.`post_id` = `p1`.`post_id` AND `p3`.`author_id` = :id3
WHERE `p1`.`author_id` = :id1