OK, get involved first 2.
SELECT * FROM table a WHERE NOT EXISTS ( SELECT 1 FROM table b WHERE b.username=a.username AND a.created_date>b.created_date )
But mysql doesn’t do very well with push predicates, hence the max-concat trick, but it will make the query very difficult - it's worth revising if you have performance problems. Now add to another material .... parts 1,3 and 5
SELECT username, level, SUM(point) FROM (SELECT * FROM table a WHERE NOT EXISTS ( SELECT 1 FROM table b WHERE b.username=a.username AND a.created_date>b.created_date ) ) ilv GROUP BY username, level HAVING SUM(point) <= 5;
How you implement 4 depends on the exact sequence in which this restriction applies to other constraints (in particular, 2 and 5). The following should produce a conclusion from the declared input ...
SELECT username, level, SUM(point) FROM (SELECT * FROM table a WHERE NOT EXISTS ( SELECT 1 FROM table b WHERE b.username=a.username AND a.created_date>b.created_date ) ) ilv, (SELECT username, SUM(point) as totpoint FROM table c GROUP BY username HAVING SUM(point)<=10) ilv2 WHERE ilv.username=ilv2.username GROUP BY username, level HAVING SUM(point) <= 5;
Oops - just read it again and see that you are not interested in seeing a breakdown of the level in the output data set - in this case Robin's answer is best.
source share