我与我的数据库有多对多的关系,所以我创建了一个sql查询,稍后我将用于搜索。 现在我想要做的是从搜索结果中获取一个唯一的值(唯一的公司名称)。
这是我的疑问:
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
结果如下:
公司,website_url,status,size,industry_id,profession_id,sector_id,seniority_id,zone_id
我想摆脱公司名称的重复。我该怎么做?
答案 0 :(得分:0)
也许您可以使用变量和子查询来做到这一点,如下所示:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
但也许在您的代码中执行此操作比在查询中更快。
答案 1 :(得分:0)
您可以使用GROUP BY
,例如:
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