我正在使用具有多个联接和group_concat的复杂查询,这是查询:
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
我的packagetypename的group_concat每个查询显示4个值,而placename仅显示where子句中给出的1个值?
因为查询是按包裹ID分组的,对于任何包裹我都有N个地方。 按逻辑查询工作正常,但我想显示所有与我对应的软件包ID的软件包
答案 0 :(得分:0)
我建议将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
答案 1 :(得分:0)
将您的查询输出修改为
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,天,价格,州名,地名,包装类型名
test1、1、3、111,xyz((katra,shiv khori),宗教
test2,2,3,444,xyz,(Katra),宗教
test3、3、3、445,xyz,NULL((家庭,夫妇,冒险,朋友)
test4,4,555,xyz,(Katra,Patnitop,sanasar,nathatop),(宗教,家庭,情侣,冒险)
test6,666,abc,NULL((家庭,冒险,朋友)
不需要包裹ID 3和6