2013-07-26 83 views
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 

我想所有我想要的IDS(入侵檢測系統列表)的公司,並與它們具有輸入的標籤「YYYY」的企業之一或表companies_main_activity_tags.val中的'xxxxx'作爲他們的主要活動。但我的查詢返回一些結果兩次或更多(取決於他們有多少標籤)

我該如何解決這個問題?

感謝

+0

你忘了啓動支架「(」爲OR條件,以及您忘記關閉支架和狀況。我有更新查詢我的回答嘗試,這將解決您的問題。 –

回答

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 
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 
+0

沒有幫助,還我得到了重複的結果 – behz4d

+0

@ behz4d我有更新我的答案嘗試更新的答案這可能是工作 –

+0

謝謝,但我已經接受了'SELECT DISTINCT' – behz4d

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