tags:

views:

30

answers:

2

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;
+2  A: 

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).

Chris Jester-Young
+4  A: 

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 
InSane
Worked perfectly. Just needed to change 'B ON A.mid' to 'B ON a.user_id'.
Eric