TAGS :Viewed: 8 - Published at: a few seconds ago

[ repeated results on mysql joins ]

Here is my query:

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

I want to get all the companies which have the ids I want(ids list) AND with companies which have one of the entered tags 'yyyy' OR 'xxxxx' in the table companies_main_activity_tags.val as their main activity. but my query returns some of the results twice and more (depends on the number of tags which they have)

How I could fix this?

Thanks

Answer 1


Try this:

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

Answer 2


Use 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

Answer 3


The DISTINCT keyword can be used to return only distinct (different) values.

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