SELECT userid,SUBSTRING_INDEX(SUBSTRING_INDEX(data, '\"!', -1), '!\"', 1)+0 AS num
FROM table
WHERE type=2
ORDER BY num DESC LIMIT 5
这样可以获得!x之间的数字!在一个字符串中,并尝试返回该数字并按该数字排序行。它工作正常。
但是,我不知道如何返回唯一的用户ID。
添加ORDER BY userid返回行而不按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
区别不起作用:
SELECT DISTINCT(userid),SUBSTRING_INDEX(SUBSTRING_INDEX(data, '\"!', -1), '!\"', 1)+0 AS num
FROM table
WHERE type=2
ORDER BY num DESC LIMIT 5
我没有想法..
示例:
id userid data
1 56 !100!
2 22 !90!
3 56 !200!
结果应该是:
userid num
56 200
22 90
答案 0 :(得分:2)
SELECT userid,MAX(num) num FROM
(SELECT userid,SUBSTRING_INDEX(SUBSTRING_INDEX(data, '\"!', -1), '!\"', 1)+0 AS num
FROM table
WHERE type=2) A
GROUP BY userid;