关于mysql连接的重复结果

时间:2013-07-26 08:58:57

标签: mysql sql join

这是我的问题:

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列表)以及在表companies_main_activity_tags.val中有一个输入标签'yyyy'OR'xxxxx'作为其主要活动的公司。但我的查询返回一些结果两次甚至更多(取决于他们拥有的标签数量)

我如何解决这个问题?

由于

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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