Sample Header Ad - 728x90

Getting rank of row

1 vote
2 answers
721 views
I am using mysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64). I am having a table referral:
CREATE TABLE referrals (
  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  referred_by bigint(20) unsigned DEFAULT NULL,
  referral_token varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  email varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  created_at timestamp NULL DEFAULT NULL,
  updated_at timestamp NULL DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY referrals_email_unique (email)
);
In the table you basically insert the person and who referred it. I have created the following leaderboard:
select
		referred_by,
		count(referred_by) as referred_by_count,
			@curRank := @curRank + 1 AS rank
from
		referrals r,
	(
	SELECT
		@curRank := 0) o
group by
		referred_by
order by
		referred_by_count DESC;
This gives me: | referred_by | referred_by_count | rank | | ----------- | ----------------- | ---- | | 10 | 3 | 3 | | 2 | 2 | 1 | | 3 | 2 | 2 | | 13 | 2 | 5 | | 11 | 2 | 6 | | 15 | 1 | 8 | | 12 | 1 | 9 | | 4 | 1 | 4 | | 9 | 1 | 7 | However, I would like to get: | referred_by | referred_by_count | rank | | ----------- | ----------------- | ---- | | 10 | 3 | 1 | | 2 | 2 | 2 | | 3 | 2 | 3 | | 13 | 2 | 4 | | 11 | 2 | 5 | | 15 | 1 | 6 | | 12 | 1 | 7 | | 4 | 1 | 8 | | 9 | 1 | 9 | [View on DB Fiddle](https://www.db-fiddle.com/f/o42zo6XiJZzEbrrVDfgQR4/3) I created the following dbfiddle using the faker php plugin. I tried using the mysql RANK() function. However, I am struggeling how to correctly use it. I appreciate your replies!
Asked by Carol.Kar (135 rep)
Dec 25, 2021, 07:30 PM
Last activity: Dec 26, 2021, 01:03 AM