SELECT userid,SUBSTRING_INDEX(SUBSTRING_INDEX(data, '\"!', -1), '!\"', 1)+0 AS num
FROM table
WHERE type=2
ORDER BY num DESC LIMIT 5
This picks up the number between !x! in a string and tries to return the number and order the rows by that number. It works fine.
However, I don’t know how to return unique user ids.
Adding ORDER BY userid returns the rows without ordering by num:
SELECT userid,SUBSTRING_INDEX(SUBSTRING_INDEX(data, '\"!', -1), '!\"', 1)+0 AS num
FROM table
WHERE type=2
GROUP BY userid
ORDER BY num DESC LIMIT 5
Distinct doesn’t work either:
SELECT DISTINCT(userid),SUBSTRING_INDEX(SUBSTRING_INDEX(data, '\"!', -1), '!\"', 1)+0 AS num
FROM table
WHERE type=2
ORDER BY num DESC LIMIT 5
I am out of ideas..
Example:
id userid data
1 56 !100!
2 22 !90!
3 56 !200!
Result should be:
userid num
56 200
22 90
1 Answer