简体   繁体   English

Mysql group_concat与众不同,其中给出奇怪的结果

[英]Mysql group_concat with distinct and where gives strange results

I have the following query which works fine (see below). 我有下面的查询工作正常(请参阅下文)。

But when I add a condition, for example AND (specialtyName = '...') the main results are fine, but the GROUP_CONCAT only shows the results that match the condition. 但是,当我添加条件时,例如AND (specialtyName = '...') ,主要结果很好,但是GROUP_CONCAT仅显示与条件匹配的结果。

Can anyone please help me with this? 谁能帮我这个忙吗?

Thanks in advance. 提前致谢。

Fred. 弗雷德

SELECT 

    tblJobs.jobID, 
    tblJobs.jobName, 
    DATE_FORMAT(tblJobs.jobDate,'%d-%m-%Y'), 

    tblCompanies.companyID, 
    tblCompanies.companyName, 
    tblCompanies.companyNameConvert, 

    GROUP_CONCAT(DISTINCT tblSpecialties.specialtyName 
        ORDER BY FIELD (
            specialtyName, 
            'specialtyName1', 
            'specialtyName2', 
            'specialtyName3'), 
            specialtyName ASC) 
        AS specialtyNames, 

    GROUP_CONCAT(DISTINCT tblSpecialties.specialtyNameConvert 
        ORDER BY FIELD (
            specialtyName, 
            'specialtyName1', 
            'specialtyName2', 
            'specialtyName3'), 
            specialtyName ASC) 
        AS specialtyNamesConvert, 

    GROUP_CONCAT(DISTINCT tblRegions.regionName), 
    GROUP_CONCAT(DISTINCT tblRegions.regionNameConvert) 

FROM tblJobs 

LEFT JOIN tblCompanies ON 
(tblJobs.jobCompany = tblCompanies.companyID) 

LEFT JOIN tblSpecialties ON 
FIND_IN_SET(tblSpecialties.specialtyID, REPLACE(tblJobs.jobSpecialty,' ',',')) 

LEFT JOIN tblRegions ON 
FIND_IN_SET(tblRegions.regionID, REPLACE(tblJobs.jobRegion,' ',','))

WHERE 

    AND jobActive = '1' 
    AND jobDate >= '2013-01-01' 
    AND companyActive = '1' 

GROUP BY jobID 
ORDER BY jobDate DESC, jobID DESC, jobCompany DESC

I suppose that using aliases for your tables and subqueries could resolve your problem. 我想为表和子查询使用别名可以解决您的问题。 You can try something like this: 您可以尝试如下操作:

SELECT 

tblJobs.jobID, 
tblJobs.jobName, 
DATE_FORMAT(tblJobs.jobDate,'%d-%m-%Y'), 

tblCompanies.companyID, 
tblCompanies.companyName, 
tblCompanies.companyNameConvert,  
(SELECT GROUP_CONCAT(DISTINCT ts.specialtyName 
        ORDER BY FIELD (
            specialtyName, 
            'specialtyName1', 
            'specialtyName2', 
            'specialtyName3'), 
            specialtyName ASC) 
        FROM tblSpecialties ts) AS specialtyNames , 
, ... ,
FROM tblJobs 

LEFT JOIN tblCompanies ON 
(tblJobs.jobCompany = tblCompanies.companyID) 

LEFT JOIN tblSpecialties ON 
FIND_IN_SET(tblSpecialties.specialtyID, REPLACE(tblJobs.jobSpecialty,' ',',')) 

LEFT JOIN tblRegions ON 
FIND_IN_SET(tblRegions.regionID, REPLACE(tblJobs.jobRegion,' ',','))

WHERE 

    AND jobActive = '1' 
    AND jobDate >= '2013-01-01' 
    AND companyActive = '1' 

GROUP BY jobID 
ORDER BY jobDate DESC, jobID DESC, jobCompany DESC

I didn't tested this code, but It could help. 我没有测试此代码,但可以提供帮助。

If you say: 如果你说:

WHERE jobActive = '1' AND jobDate >= '2013-01-01' AND companyActive = '1' AND
      specialties = XXX

Then you are only going to get exactly those specialties. 然后,您将只获得这些专业。 The filtering is done before the aggregation. 过滤是在聚合之前完成的。 As a note: including such conditions in the where clause also turns the outer joins to inner joins. 注意:在where子句中包含这样的条件也会将外部联接转换为内部联接。 Your joins are probably on properly aligned foreign key relationships, so inner joins may be appropriate. 您的联接可能基于正确对齐的外键关系,因此内部联接可能是合适的。

I'm guessing what you really want is to filter jobs by those having that specialty, but to keep all other information. 我猜您真正想要的是按具有该专业的人员来过滤工作,但保留所有其他信息。 You want to do the filtering after the aggregation. 您要在聚合进行过滤。 Do this with a having clause instead of a where clause: 使用having子句代替where子句来做到这一点:

having sum(specialties = XXX) > 0;

This will keep only the rows that have the particular specialty, and keep all the other information. 这将仅保留具有特定专业的行,并保留所有其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM