2015-08-28 144 views
0

创建正确的索引我有表:如何狮身人面像

product 
| id | shop_id | name | 
| 1 | 1  | p1 | 
| 2 | 1  | p2 | 
| 3 | 2  | p3 | 
etc 

shop 
| id | name | 
| 1 | s1 | 
| 2 | s2 | 
| 3 | s3 | 
| 4 | s4 | 
etc 

country 
| id | name | 
| 1 | russia | 
| 2 | usa | 
etc 

shop_country 
|id | shop_id | country_id | 
| 1 | 1  | 1  | 
| 2 | 1  | 2  | 
| 3 | 2  | 1  | 

我需要在一组国家供应的商品。

这是从MySQL工作请求:

SELECT * 
FROM product AS p 
LEFT JOIN shop_country sc ON (p.shop_id = sc.shop_id) 
WHERE product.status = 1 mc.country_id = 3 

我创建一个索引:

SELECT product.id as id, product.shop_id \ 
FROM product \ 
LEFT JOIN shop_country sc ON (product.shop_id = sc.shop_id) \         
GROUP BY product.id 

,但它会产生错误的结果

什么我需要在狮身人面像得到这个?

回答

1
GROUP BY product.id 

意味着你有很多国相同产品

SELECT product.id as id, product.shop_id \ 

没有组功能

因此,通过

SELECT product.id as id, group_concat(product.shop_id) \ 

更换和尝试设置 shop_id为sql_attr_multi