Reputation: 138
I'm trying to filter the table collections by id and then join the collections.user_id on users.user_id.
Here is the query I've tried
SELECT * FROM
(SELECT * FROM collections WHERE mid=`$id`) c
JOIN users on c.mid = users.user_id ORDER BY users.user_id
Haven't managed to get it work obviously. Any help or pointers would be appreciated!
CREATE TABLE `collections` (
`user_id` int(11) NOT NULL,
`mid` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`api_public` varchar(32) DEFAULT NULL,
`api_secret` varchar(32) NOT NULL,
UNIQUE KEY `unique id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
Upvotes: 2
Views: 194
Reputation: 10356
Can you try this?
SELECT *
FROM collections A
JOIN users B ON A.mid = B.user_id
AND A.mid = 'value of $id'
ORDER BY B.user_id
Upvotes: 4
Reputation: 223023
How about this:
SELECT c.*, u.* FROM collections c
JOIN users u ON c.mid = ? AND c.mid = u.user_id
ORDER BY u.user_id
(where ?
is the parameter marker, of course).
Upvotes: 2