如何获得具有多个指定ID之一的每一行?

时间:2016-02-27 23:33:37

标签: mysql database where-clause where-in

我有(1,2,3,4,5,20,25,27)个值id的列表。我想获得具有这些值中的任何一个的所有行。

所以我决定用WHERE条款来获取它。

SELECT DISTINCT c.id, IF(c.image IS NULL, "default.png", c.image), 
c.first_name, c.email_primary, c.contact_mobile, c.contact_home, 
c.last_name, c.contact_work, cn.country_name, c.unique_id, c.title, 
cm.name, 
SUBSTRING(c.first_name, 1, 1), IF(s.id IS NULL, "0", "1") flag_already_synced, 
GROUP_CONCAT(t.tag) contact_tag_names, sector, c.salutation, c.email_secondry,
c.email_work, c.contact_work, REPLACE(c.assistant_name, "\r\n", ""), 
c.city, h.honorofic, REPLACE(c.contact_owner, "\r\n", ""), c.created_on, c.gender, c.flag_vip 
FROM (`contact` c) 
LEFT JOIN `country` cn ON `cn`.`id`=`c`.`country_id` 
LEFT JOIN `sync` s ON `s`.`contact_id` = `c`.`id` AND s.user_id = 29 
LEFT JOIN `contact_tag` ct ON `ct`.`contact_id` = `c`.`id` 
LEFT JOIN `tag_master` t ON `ct`.`tag_id` = `t`.`id` 
LEFT JOIN `honorfic` h ON `h`.`id` = `c`.`honorfic` 
LEFT JOIN ( select company_id,contact_id from 
contact_company where flag_primary = "Y" AND flag_delete = "0" order by 
created_on desc ) as cc ON `c`.`id`=`cc`.`contact_id` 
LEFT JOIN `company` cm ON `cm`.`id`=`cc`.`company_id` 
WHERE `c`.`flag_delete` = '0' 
AND (c.flag_vip=0 or 
c.flag_vip=1 or c.flag_vip= 2) 
AND `c`.`guest_of` IS NULL AND `c`.`id` IN ( 
'1','2','3','4','5','20','25','27' ) 
LIMIT 50

但是我只得到一行,id等于1.我应该怎样做才能获得所有请求的id值的所有行?

c.id IN ( '1','2','3','4','5','20','25','27' )似乎无法获得具有上述id s`的所有行

修改

在评论建议之后我删除了所有其他WHERE子句条件并离开了c.id IN ( 1,2,3,4,5,20,25,27 )但是我仍然只有一行。

0 个答案:

没有答案