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 (ID リスト) を持つすべての会社と、テーブルに入力されたタグ「yyyy」または「xxxxx」のいずれかをcompanies_main_activity_tags.val主なアクティビティとして持つ会社をすべて取得したいと考えています。しかし、私のクエリは結果の一部を2回以上返します(持っているタグの数によって異なります)

どうすればこれを修正できますか?

ありがとう

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 に答える