I’m trying to grab a user_id from my users tables that has the most referrals. Sorry for the vague title, I’m not sure what this is called.
Using PHP and MYSQL.
Example:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(180) CHARACTER SET utf8 NOT NULL,
`ip` varchar(180) CHARACTER SET utf8 NOT NULL,
`registered` varchar(180) CHARACTER SET utf8 NOT NULL,
`lastonline` varchar(180) CHARACTER SET utf8 NOT NULL,
`referral` varchar(180) CHARACTER SET utf8 NOT NULL,
`url` varchar(180) CHARACTER SET utf8 NOT NULL,
`points` float NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6544 ;
A user will look like this,
1, 'username', 'ipaddy', 'date', 'date', '**REFERRAL**', 'url', 13633
I’m trying to find the user_id that has the most of the same referral in all of the users tables. Lets say user 4 has 10 different users with the same thing in the referral varchar. How can i select see user 4 without going through every single user, logging their referrals, and then pick the one with the most.
There has to be an easier way…
Any help is appreciated. Thanks!
EDIT: Sorry, referral is the name of another user, not the user_id.
A little extra added to your SQL: