Php 使用组连接和类别搜索进行多类别连接
我需要通过搜索具有特定类别的业务来查找具有group concat的业务的所有类别Php 使用组连接和类别搜索进行多类别连接,php,mysql,sql,Php,Mysql,Sql,我需要通过搜索具有特定类别的业务来查找具有group concat的业务的所有类别 |------ business -------- | |-business_id |-business_name |-business_city |------ business_cat_map ----| |-id |-business_id |-cat_id |------ cat --------------| |-cat_id |-cat_name 这是简单的多类别关系 现在,我想查询类别为(比如
|------ business -------- |
|-business_id
|-business_name
|-business_city
|------ business_cat_map ----|
|-id
|-business_id
|-cat_id
|------ cat --------------|
|-cat_id
|-cat_name
这是简单的多类别关系
现在,我想查询类别为(比如酒店)的企业的数据库,并使用group_concat()返回该企业所属的所有猫的详细信息
我在玩这种查询,但似乎不起作用
SELECT a.*
GROUP_CONCAT(c.cat_name) as cats
FROM business a
INNER JOIN business_cat_map b
ON a.business_id = b.business_id
INNER JOIN cat c
ON c.cat_id = b.cat_id
WHERE cat_name = 'Motels'
GROUP BY a.business_id
我获得了所需的业务,但只获得了集团中的汽车旅馆类别。\u concat()[显然是由于条件cat\u name='Motels']
请有人告诉我如何在一个查询中实现这一点。我不想使用2个查询
这是数据
|------------业务------------|
|商业| id |商业|名称|商业|城市
|------商业类地图--------------|
|------身份证----------商务身份证----------cat身份证--|
|--------猫----------------|
|-----id---------猫名----|
我的查询返回以下结果
商业| id |-商业|名称|-猫--|
因此,这里我只得到一只猫,但公司有不止一只猫,我想在猫栏中显示这些猫
SELECT a.*
GROUP_CONCAT(c.b_type_name) as cats
FROM business a
INNER JOIN business_cat_map b
ON a.business_id = b.business_id
INNER JOIN cat c
ON c.cat_id = b.cat_id
WHERE a.business_id in (
select x.business_id from business_cat_map AS x where x.cat_id = (
select y.cat_id from cat AS y where y.cat_name = 'Hotels'
)
)
GROUP BY a.business_id
SELECT a. * , GROUP_CONCAT( c.b_type_name )
FROM business a
INNER JOIN business_what b ON b.business_id = a.business_id
INNER JOIN business_type c ON c.b_type_id = b.b_type_id
WHERE b.business_id
IN (
SELECT business_what.business_id
FROM business_type
JOIN business_what ON business_type.b_type_id = business_what.b_type_id
WHERE business_type.b_type_name LIKE '%Hotel%'
)
GROUP BY a.business_id
伙计,这是你的安塞
SELECT a. * , GROUP_CONCAT( c.b_type_name )
FROM business a
INNER JOIN business_what b ON b.business_id = a.business_id
INNER JOIN business_type c ON c.b_type_id = b.b_type_id
WHERE b.business_id
IN (
SELECT business_what.business_id
FROM business_type
JOIN business_what ON business_type.b_type_id = business_what.b_type_id
WHERE business_type.b_type_name LIKE '%Hotel%'
)
GROUP BY a.business_id
请显示具有所需结果的样本记录表格中有c.b_类型_名称吗?您能用一些样本数据发布当前查询输出和所需输出吗..SQLFIDLE会很好-与所需结果一起使用Orry b_类型_名称是cat_名称我的不好请显示具有所需结果的样本记录表格中有c.b_类型_名称表?你能用一些示例数据发布当前查询输出和所需的输出吗..SQLFIDLE会很好-连同所需的结果一起使用Orry b_type_name是cat_name我的不好谢谢你的回答,但是查询没有返回行谢谢你的回答,但是查询没有返回行