SELECT *
FROM (
SELECT `at`.`id_article` AS `id_article`, COUNT(*) AS `count`
FROM `article_tags` AS `at`
JOIN `tags` AS `t` ON `t`.`id` = `at`.`id_tag`
AND `t`.`name` IN ('Тег_10', 'Тег_11', 'Тег_12')
GROUP BY `id_article`
) AS `c`
JOIN `articles` AS `a` ON `a`.`id` = `c`.`id_article`
ORDER BY `c`.`count` DESC
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`
UPDATE `users`
LEFT JOIN (
SELECT `user_id_from`, COUNT(*) AS `count` FROM `messages` GROUP BY `user_id_from`
) AS `from` ON `from`.`user_id_from` = `users`.`user_id`
LEFT JOIN (
SELECT `user_id_to`, COUNT(*) AS `count` FROM `messages` GROUP BY `user_id_to`
) AS `to` ON `to`.`user_id_to` = `users`.`user_id`
SET `users`.`sended` = IFNULL(`from`.`count`, 0),
`users`.`received` = IFNULL(`to`.`count`, 0)
UPDATE `address` AS `a1`
JOIN (
SELECT MIN(`id`) AS `id` FROM `address` GROUP BY `organizationfk`
) AS `a2` ON `a2`.`id` = `a1`.`id`
LEFT JOIN (
SELECT `organizationfk` FROM `address` WHERE `primary` = 1
) AS `a3` ON `a3`.`organizationfk` = `a1`.`organizationfk`
SET `a1`.`primary` = 1
WHERE `a3`.`organizationfk` IS NULL
CREATE DATABASE `_mo`;
CREATE DATABASE IF NOT EXISTS `_mo`;
CREATE DATABASE IF NOT EXISTS `_mo` DEFAULT CHARACTER SET latin1;