2013-11-01 36 views
0

我需要通過特定類別搜索業務找到與組CONCAT業務的所有類別多類別與GROUP_CONCAT和類別搜索論壇

|------ 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 

我獲得所需要的業務,但只在GROUP_CONCAT得到汽車旅館類別()[顯然由於條件cat_name ='汽車旅館]

所以PLZ有人告訴我如何做到這一點單查詢。我不想使用2個查詢。

這裏是數據

| ------------ 業務 -------------- |

| business_id | BUSINESS_NAME | business_city

enter image description here

| ------ business_cat_map -------------- |

| ------ ID ------ | --- business_id --- | --cat_id-- |

enter image description here

| -------- ---------------- |

| ----- ----- ID | ------ cat_name ---- |以下

business_id

enter image description here

我的查詢返回| -business_name- | --cats-- |

enter image description here

所以在這裏我只是得到一個貓,但企業都有,我想在貓列中顯示一個以上的貓

+0

請出示樣本記錄你想要的結果 –

+0

哪裏是c.b_type_name在你的表? – Jim

+0

您可以發佈當前查詢輸出和您需要的輸出與一些示例數據。 – Prashanth

回答

0

多德這裏是你的雁

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 
+0

像魅力的感謝 – themightysapien

0
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 
+0

感謝您的答案,但查詢返回沒有行 – themightysapien