我想在结果中按id显示不同的记录,但它不起作用。 我使用了以下查询,
SELECT distinct(b.`id`), b.`from_name`, b.`posted_on`, b.`subject`, a.`to` FROM `message` as b, `message_recepient` as a WHERE b.`id` in (15063,15106,15075,15170) AND b.`id` = a.`msg_id`
按查询返回的数据是
15063 John Doe 2013-03-18 07:18:15 hello 59640
15075 Dan Patrik 2013-04-09 06:27:04 hiiiiii 59640
15106 Rahul Y 2013-04-17 13:35:27 good morning 50452
15106 Rahul Y 2013-04-17 13:35:27 good morning 55129
15106 Rahul Y 2013-04-17 13:35:27 good morning 59610
15106 Rahul Y 2013-04-17 13:35:27 good morning 59622
15106 Rahul Y 2013-04-17 13:35:27 good morning 59640
15170 Ritesh D 2013-04-24 05:02:56 hi 59640
答案 0 :(得分:1)
尝试加入
SELECT distinct(b.`id`),b.`from_name`,b.`posted_on`,b.`subject`, a.`to` FROM `message` as b inner join `message_recepient` as a on (b.`id` = a.`msg_id`) WHERE b.`id` in (15063,15106,15075,15170)
或,
SELECT b.`id`,b.`from_name`,b.`posted_on`,b.`subject`, a.`to` FROM `message` as b inner join `message_recepient` as a on (b.`id` = a.`msg_id`) WHERE b.`id` in (15063,15106,15075,15170) group by id