I have the following scheme:
CREATE TABLE IF NOT EXISTS `connections` (
`username1` varchar(15) NOT NULL,
`username2` varchar(15) NOT NULL,
PRIMARY KEY (`username1`,`username2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `connections` (`username1`, `username2`) VALUES
('guy', 'maricela'),
('maricela', 'guy'),
('mikha', 'guy');
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(15) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `users` (`username`, `first_name`, `last_name`) VALUES
('maricela', 'Maricela', 'Ros'),
('mikha', 'Michael', 'Samuel'),
('guy', 'Guy', 'Marquez');
The scheme contains 2 tables; one for user info called users
with info first name,last name, and username. The 2nd table is connections. The column connections.username1
is the one who follows and connections.username2
is the one who is followed. I want to output a list of the people who follow the user maricela
and for each result check if the resulting user is followed by the user mikha
or not.
I use the following query but checking if the result is followed by mikha
isn't working:
SELECT followed_by_mikha.username AS followed_by_mikha,
users.first_name,
users.last_name,
users.username
FROM connections
LEFT JOIN users ON users.username = connections.username1
LEFT JOIN users followed_by_mikha ON (connections.username1 = 'mikha' AND connections.username2 = users.username AND connections.username2 = followed_by_mikha.username)
WHERE connections.username2 = 'maricela'
ORDER BY users.username DESC LIMIT 10
Thanks :)