开发者

Displaying most common correlations in a MySQL table

开发者 https://www.devze.com 2023-03-03 04:18 出处:网络
I have a table with three fields - userID, couponID, latest_couponID. For the last two fields, there could be pairs such as 1 & 3, 5 &a开发者_开发问答mp; 9, 10 & 3, just for example. I\'m tryi

I have a table with three fields - userID, couponID, latest_couponID. For the last two fields, there could be pairs such as 1 & 3, 5 &a开发者_开发问答mp; 9, 10 & 3, just for example. I'm trying to find the most common pairs, and the amount of occurrences, and list them in order. How can I do that?


select couponID, latest_couponID, count(*) as occurances from your_table group by couponID, latest_couponID ORDER BY occurances DESC;


SELECT userID, couponID, latest_couponID, count(*) AS freq
FROM table
GROUP BY couponID, latest_couponID
ORDER BY freq DESC;

Formatting code

0

精彩评论

暂无评论...
验证码 换一张
取 消