开发者

Getting unique values as a query result

开发者 https://www.devze.com 2023-04-05 03:46 出处:网络
I have a many-to-many relation with my database, so I created a sql query which later I will use for search.

I have a many-to-many relation with my database, so I created a sql query which later I will use for search. Now what I want to do is get a unique values from my search result (unique company names).

This is my query:

SELECT agencies.company
    ,agencies.website_url
    ,agencies.STATUS
    ,agencies.size
    ,IndustryData.industry_id
    ,ProfessionData.profession_id
    ,SectorData.sector_id
    ,seniorityData.seniority_id
    ,ZonesData.zone_id
FROM agencies
LEFT JOIN (
    SELECT agencies_industries.agency_id
        ,agencies_industries.industry_id
    FROM agencies_industries
    ) AS IndustryData ON agencies.id = IndustryData.agency_id
LEFT JOIN (
    SELECT agencies_professions.agency_id
        ,agencies_professions.profession_id
    FROM agencies_professions
    ) AS ProfessionData ON agencies.id = ProfessionData.agency_id
LEFT JOIN (
    SELECT agencies_sectors.agency_id
        ,agencies_sectors.sector_id
    FROM agencies_sectors
    ) AS SectorData ON agencies.id = SectorData.agency_id
LEFT JOIN (
    SELECT agencies_seniorities.agency_id
        ,agencies_seniorities.seniority_id
    FROM agencies_seniorities
    ) AS SeniorityData ON agencies.id = SeniorityData.agency_id
LEFT JOIN (
    SELECT agencies_zones.agency_id
        ,agencies_zones.zone_id
    FROM agencies_zones
    ) AS ZonesData ON agencies.id = ZonesData.agency_id
WHERE IndustryData.industry_id = 3
    AND ProfessionData.profession_id = 1

The result looks like this:

company, website_url, status, size, industry_id, profession_id, sector_id, seniority_id, zone_id

  1. Nine nine.com 1 3 3 1 3 2 1
  2. Nine nine.com 1 3 3 1 3 2 5
  3. Nine nine.com 1 3 3 1 3 2 8
  4. Nine nine.com 1 3 3 1开发者_开发百科 3 5 1
  5. Nine nine.com 1 3 3 1 3 5 5
  6. Nine nine.com 1 3 3 1 3 5 8
  7. Ten ten.com 2 3 3 1 3 1 1
  8. Ten ten.com 2 3 3 1 3 1 3
  9. Ten ten.com 2 3 3 1 3 1 7
  10. Ten ten.com 2 3 3 1 3 3 1
  11. Ten ten.com 2 3 3 1 3 3 3
  12. Ten ten.com 2 3 3 1 3 3 7
  13. Ten ten.com 2 3 3 1 3 5 1
  14. Ten ten.com 2 3 3 1 3 5 3
  15. Ten ten.com 2 3 3 1 3 5 7

I would like to get rid of the repeats of the company names. How do I do that?


You could use a GROUP BY for this, e.g.:

SELECT agencies.company
    ,agencies.website_url
    ,agencies.STATUS
    ,agencies.size
    ,IndustryData.industry_id
    ,ProfessionData.profession_id
    ,SectorData.sector_id
FROM ...
GROUP BY agencies.company
    ,agencies.website_url
    ,agencies.STATUS
    ,agencies.size
    ,IndustryData.industry_id
    ,ProfessionData.profession_id
    ,SectorData.sector_id


Maybe you could do that with variables and subqueries, as some examples shown here:

http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

But maybe doing this at your code is faster than in your query.

0

精彩评论

暂无评论...
验证码 换一张
取 消