简体   繁体   中英

Filtering table and then joining results

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;

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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM