I'm trying to write a friend system that works like this:
- User A sends friend request to user B.
- User B accepts friend request from user A.
- Users A and B are now friends.
Here is my database structure:
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(16) NOT NULL, `password` char(32) NOT NULL, `email` varchar(80) NOT NULL, `dname` varchar(24) NOT NULL, `profile_img` varchar(255) NOT NULL DEFAULT '/images/default_user.png', `created` int(11) NOT NULL, `updated` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`), UNIQUE KEY `username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
CREATE TABLE IF NOT EXISTS `friend_requests` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_a` int(11) NOT NULL DEFAULT '0', `user_b` int(11) NOT NULL DEFAULT '0', `viewed` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
CREATE TABLE IF NOT EXISTS `friends` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_a` int(11) NOT NULL DEFAULT '0', `user_b` int(11) NOT NULL DEFAULT '0', `friend_type` int(3) NOT NULL DEFAULT '1', `friends_since` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I can list a users friends with "SELECT * FROM friends WHERE user_a = $userid OR user_b = $userID", but how can I get data such as username or profile_img from the users table?
SELECTstatement or anINNER JOIN. - ComFreek