【发布时间】:2014-02-08 18:34:50
【问题描述】:
你好 stackoverflow 社区 :) ,
我有一个复杂的连接查询给我带来了很多麻烦:/
我这里有 3 张桌子。
1:表[分类法t]
id ownerId type
1 1 office
2 1 inventory
3 1 inventory_item
2:表 [tax_links l]
id parent son
1 1 2
2 1 3
3:表[设置s]
id taxId title value type
1 1 name office1 taxonomy
2 1 location Address taxonomy
3 1 settings on tax_links
所以 1. 分类表包含用户的所有资源 2. link_taxs 将 2 个分类法相互链接 3. 设置保存资源设置,如果我希望设置与关系相关(不是全局),我将设置类型设置为 tax_links
我的查询应该返回用户的所有资源,并将所有相关的集中为儿子,以及relsId中的关系ID。
SELECT `t`.`id`, group_concat(l.son) as sons, group_concat(l.id) as relsId, group_concat( s.title ) as titles, group_concat( s.value ) as vals, `t`.`name`, `t`.`type`, group_concat(s.id) as sid
FROM (`taxonomys` t)
LEFT JOIN `tax_links` l ON `l`.`parent` = `t`.`id`
LEFT JOIN `settings` s ON `s`.`taxId` = `t`.`id` and s.table = 'taxonomy'
WHERE `t`.`ownerId` = 1
GROUP BY `t`.`id`
它运行完美,并返回我需要的所有内容除了它在儿子、relsId 中返回 REPLICATED 结果。
例如我在运行此查询时提供的表,我希望结果是
id sons relsId titles vals
1 2,3 1,2 name,location office1,address
问题是当我运行查询时,它会返回儿子和 relsId 的重复内容,所以我得到类似
id sons relsId titles vals
1 2,3,2,3 1,2 name,location office1,address
name,location office1,address
为什么会这样?我知道我可以在获取行后使用 php 过滤 array_unique,但是我做错了什么?
【问题讨论】:
标签: php mysql sql join concatenation