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 s
join qz_users u on u.id=s.user
join qz_user_stat us on us.user=s.user
left join qz_user_stat_date s2 on s2.user=s.user and s2.date='.$today.'
left join qz_user_stat_date s3 on s3.user=s.user and s3.date='.$yest.'
left join qz_user_stat_date s4 on (
s4.user=s.user
and s4.date = (
select min(date) from qz_user_stat_date
where user = s4.user
and OCI = (
select max(OCI) from qz_user_stat_date
WHERE user = s4.user
)
)
)
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`