0

我有以下查询可以正常工作(见下文)。

但是当我添加一个条件时,例如AND (specialtyName = '...')主要结果很好,但 GROUP_CONCAT 只显示与条件匹配的结果。

谁能帮我解决这个问题?

提前致谢。

弗雷德。

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
4

2 回答 2

0

我想为您的表和子查询使用别名可以解决您的问题。你可以尝试这样的事情:

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

我没有测试此代码,但它可以提供帮助。

于 2014-05-22T10:09:49.377 回答
0

如果你说:

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

那么你只会得到这些专业。过滤是在聚合之前完成的。注意:在子句中包含这样的条件where也会将外连接变为内连接。您的联接可能在正确对齐的外键关系上,因此内部联接可能是合适的。

我猜您真正想要的是按具有该专业的人过滤工作,但保留所有其他信息。您想在聚合后进行过滤。having使用子句而不是子句来执行此where操作:

having sum(specialties = XXX) > 0;

这将只保留具有特定专业的行,并保留所有其他信息。

于 2014-05-22T11:54:57.757 回答