I am trying to get results from mysql by not letting a specific field gets repeated more than 5 times.
For example, lets say that I have a table like this :
-------------
Name City
person1 Nashville
person2 Nashville
person3 Nashville
person4 Nashville
person5 Nashville
person6 Nashville
person7 New York
-------------
What I want it to return is this :
-------------
Name City
person1 Nashville
person2 Nashville
person3 Nashville
person4 Nashville
person5 Nashville
person7 New York
-------------
where the city field cannot be repeated more than five times! if it was repeated more than fix times, only return the first five results.
This question could be impossible (that is what I am guessing) but if there is a solution or anyway to work around what I am trying to achieve, please tell me! I can use PHP.
How about something like this which uses variables to check the current row to the previous row, if they are the same then the row number will increase if they are not the row number will reset:
See SQL Fiddle with Demo
The result is: