0

这是我的查询:

SELECT companies.id, companies.business_number, companies.country_iso_3, companies.profile_img, companies.short_url, companies_details_en.company_name
FROM companies
JOIN companies_details_en ON companies_details_en.company_id = companies.id
LEFT JOIN companies_main_activity_tags ON companies_main_activity_tags.company_id = companies_details_en.company_id
WHERE (
companies.id = '1'
OR companies.id = '3'
OR companies.id = '4'
OR companies.id = '5'
OR companies.id = '7'
OR companies.id = '20'
OR companies.id = '21'
OR companies.id = '22'
)
AND ((companies_main_activity_tags.val LIKE '%xxxx%') OR (companies_main_activity_tags.val LIKE '%yyyy%') OR companies_main_activity_tags.lang = 'en')
AND companies.id = companies_details_en.company_id
AND companies.id = companies_main_activity_tags.company_id
LIMIT 0 , 30

我想获取所有具有我想要的 id 的公司(ids 列表)以及在表中具有输入标签“yyyy”或“xxxxx”之一的公司companies_main_activity_tags.val作为他们的主要活动。但我的查询返回一些结果两次甚至更多(取决于他们拥有的标签数量)

我该如何解决这个问题?

谢谢

4

3 回答 3

1

尝试这个:

SELECT DISTINCT companies.id, companies.business_number, companies.country_iso_3, companies.profile_img, companies.short_url, companies_details_en.company_name
FROM companies
JOIN companies_details_en ON companies_details_en.company_id = companies.id
LEFT JOIN companies_main_activity_tags ON companies_main_activity_tags.company_id = companies_details_en.company_id
WHERE (
companies.id = '1'
OR companies.id = '3'
OR companies.id = '4'
OR companies.id = '5'
OR companies.id = '7'
OR companies.id = '20'
OR companies.id = '21'
OR companies.id = '22'
)
AND ((companies_main_activity_tags.val LIKE '%xxxx%') OR (companies_main_activity_tags.val LIKE '%yyyy%') OR (companies_main_activity_tags.lang = 'en'))
AND companies.id = companies_details_en.company_id
AND companies.id = companies_main_activity_tags.company_id
LIMIT 0 , 30
于 2013-07-26T09:04:49.247 回答
1

利用GROUP BY

SELECT c.id, c.business_number, c.country_iso_3, c.profile_img, c.short_url, c.company_name
  FROM companies c JOIN companies_details_en d 
    ON d.company_id = c.id LEFT JOIN companies_main_activity_tags t 
    ON t.company_id = d.company_id
WHERE c.id IN (1, 3, 4, 5, 7, 20, 21, 22)
  AND (   t.val LIKE '%xxxx%' 
       OR t.val LIKE '%yyyy%' 
       OR t.lang = 'en')
 GROUP BY c.id, c.business_number, c.country_iso_3, c.profile_img, c.short_url, c.company_name
LIMIT 0, 30
于 2013-07-26T09:05:18.693 回答
1

DISTINCT关键字可用于仅返回不同(不同)的值。

SELECT distinct companies.id, companies.business_number, companies.country_iso_3, companies.profile_img, companies.short_url, companies_details_en.company_name
FROM companies
JOIN companies_details_en ON companies_details_en.company_id = companies.id
LEFT JOIN companies_main_activity_tags ON companies_main_activity_tags.company_id = companies_details_en.company_id
WHERE (
companies.id = '1'
OR companies.id = '3'
OR companies.id = '4'
OR companies.id = '5'
OR companies.id = '7'
OR companies.id = '20'
OR companies.id = '21'
OR companies.id = '22'
)
AND ((companies_main_activity_tags.val LIKE '%xxxx%') OR (companies_main_activity_tags.val LIKE '%yyyy%') OR companies_main_activity_tags.lang = 'en')
AND companies.id = companies_details_en.company_id
AND companies.id = companies_main_activity_tags.company_id
LIMIT 0 , 30
于 2013-07-26T09:33:26.173 回答