group\u concat在使用where子句时没有显示所有值

xriantvc  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(406)

我正在使用一个包含多个联接和组连接的复杂查询,下面是查询:

select a.packagename as packagename, a.packageid as packageid, 
 sum(a.packageduration) as days, (a.packageprice + a.seater_4) as price,
 b.statename,
 substring_index(GROUP_CONCAT( DISTINCT (select f.placename )  SEPARATOR ', 
 '),',',4) placename,
 substring_index(GROUP_CONCAT( DISTINCT (select d.packagetypename )  
 SEPARATOR ', '),',',4) packagetypename
 from tblpackages a
 join tblstates b on b.stateid = a.stateid              
 join tblpackage_packagetypes c on c.packageid = a.packageid
 join tblpackagetype d on d.packagetypeid = c.packagetypeid
 join tblpackageplaces e on e.packageid = a.packageid
 join tblplaces f on f.placeid = e.placeid
                where d.placename = "placename"
                 group by a.packageid

我的分组\u concat for packagetypename在每个查询中显示4个值,而placename只显示where子句中给出的1个值?
由于查询是按包id分组的,对于任何包,我有n个位置。。。。查询按逻辑正常工作,但我想显示对应包id的所有包

t8e9dugd

t8e9dugd1#

带位修改的OUR查询输出

select a.packagename as packagename, a.packageid as packageid, 
a.packageduration as days, (a.packageprice + a.seater_4) as price,
b.statename,
substring_index(GROUP_CONCAT( DISTINCT (select f.placename )  SEPARATOR ', '),',',4) 
placename,
substring_index(GROUP_CONCAT( DISTINCT (select d.packagetypename )  SEPARATOR ', 
'),',',4) packagetypename
from tblpackages a
inner join tblstates b on b.stateid = a.stateid              
inner join tblpackage_packagetypes c on c.packageid = a.packageid
inner  join tblpackagetype d on d.packagetypeid = c.packagetypeid
inner join tblpackageplaces e on e.packageid = a.packageid
left join tblplaces f on f.placeid = e.placeid  and d.packagetypename = 'religious'
                 group by a.packageid

pname,pid,days,price,statename,placename,packagetypename
测试1,1,3,111,xyz,(katra,shiv khori),宗教
测试2,2,3,444,xyz,(katra),宗教
test3,3,3,445,xyz,null,(家庭,夫妻,冒险,朋友)
测试4,4,555,xyz,(katra,patnitop,sanasar,nathatop),(宗教,家庭,夫妻,冒险)
test6,666,,null,(家庭,冒险,朋友)
不需要包id 3和6

fnatzsnv

fnatzsnv2#

我建议把标准移到 WHERE 条款 ON 子句,并使该联接成为左联接:

SELECT ...
FROM tblpackages a
INNER JOIN tblstates b
    ON b.stateid = a.stateid              
INNER JOIN tblpackage_packagetypes c
    ON c.packageid = a.packageid
LEFT JOIN tblpackagetype d
    ON d.packagetypeid = c.packagetypeid AND d.placename = 'placename'
INNER JOIN tblpackageplaces e
    ON e.packageid = a.packageid
INNER JOIN tblplaces f
    ON f.placeid = e.placeid
GROUP BY
    a.packageid

相关问题