【发布时间】:2011-03-22 15:46:32
【问题描述】:
我有 5 张桌子。
先给products点赞:
id | country_ids | category_ids | users_ids
1 | 1,4,6 | 4,5,6,70 | 5,6,9
2 | 5,6,3 | 4,8,2,11 | 1,5,8
第二个countries点赞:
c_id | c_name
1 | Åland Islands
2 | Antarctica
...
第三个categories点赞:
cat_id | cat_name
2 | Small
4 | Large
...
第四桌users赞:
u_id | u_name
1 | David
2 | Mary
...
第五张表review(表结构不重要,只计算id)。
和sql
SELECT a.*, COUNT(b.comm_id) AS comm_count, c.*, d.*, e.* FROM 产品作为 LEFT JOIN cmets AS b ON b.comm_prod_id = a.id AND b.comm_published = 1 LEFT JOIN countries AS c ON c.c_id IN (a.country_ids) LEFT JOIN 类别 AS d ON d.c_id IN (a.category_ids) LEFT JOIN users AS e ON e.c_id IN (a.users_ids) /*发布地点 = 1*/ 按 ID 分组 ORDER BY id DESC 限制 0, 5但是这个查询只返回连接表的第一个值。
我怎样才能得到这样的行
1 | Åland Islands, Equador, Russia | Small, tiny, large, ... | Anna, John, Linda
PS!还是我需要为每个表创建表关系?什么很不喜欢。
【问题讨论】: