Displaying most common correlations in a MySQL table
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
精彩评论