我有三个表:
id | name
----+------
1 | a
2 | b
3 | c
4 | d
branch_1
id | parent_id | b1_name
----+-----------+---------
1 | 1 | aaa
2 | 1 | bbb
3 | 1 | ccc
4 | 2 | ddd
5 | 2 | eee
6 | 2 | fff
7 | 2 | ggg
8 | 2 | hhh
branch_2
id | b2_name | parent_id
----+---------+-----------
1 | b2_aa | 1
2 | b2_ba | 2
3 | b2_bb | 2
4 | b2_bc | 2
我只想将这些表与唯一值结合使用。我尝试了这段代码:
select p.id, b1.b1_name, b2.b2_name
from parent p
left outer join lateral (select distinct b1.b1_name from branch_1 b1 where b1.parent_id=p.id order by b1.b1_name) as b1 on true
left outer join lateral (select distinct b2.b2_name from branch_2 b2 where b2.parent_id=p.id order by b2.b2_name) as b2 on true
但结果中有重复的值:
id | b1_name | b2_name
----+---------+---------
1 | aaa | b2_aa
1 | bbb | b2_aa
1 | ccc | b2_aa
2 | ddd | b2_ba
2 | ddd | b2_bb
2 | ddd | b2_bc
2 | eee | b2_ba
2 | eee | b2_bb
2 | eee | b2_bc
2 | fff | b2_ba
2 | fff | b2_bb
2 | fff | b2_bc
2 | ggg | b2_ba
2 | ggg | b2_bb
2 | ggg | b2_bc
2 | hhh | b2_ba
2 | hhh | b2_bb
2 | hhh | b2_bc
我想得到:
id | b1_name | b2_name
----+---------+---------
1 | aaa | b2_aa
1 | bbb | ---
1 | ccc | ---
2 | ddd | b2_ba
2 | eee | b2_bb
2 | fff | b2_bc
2 | ggg | ---
2 | hhh | ---
也许看起来很奇怪,但是我只需要唯一的值而不重复。那么,我如何只能拥有唯一的价值?
P.S。我需要一个用于在狮身人面像上进行小平面搜索的表,这样才能正确考虑产品数量,并发出这样的请求:
select * from facetIndex facet b1_name, b2_name
答案 0 :(得分:1)
我想知道狮身人面像多值属性是否可以解决这个问题。 they,它们只是整数。因此可以将分支表ID放入MVA(而不是名称)中
如果PostgreSQL拥有group_concat
也许可以做到
sql_query = SEELCT p.id,
GROUP_CONCAT(DISTINCT b1.id) as b1,
GROUP_CONCAT(DISTINCT b2.id) as b2
FROM parent p
LEFT JOIN branch1 b1 ON (b1.parent = p.id)
LEFT JOIN branch2 b2 ON (b2.parent = p.id)
GROUP BY p.id
但是好吗?
...因此,请使用sql_attr_multi
功能来运行单独的查询。
sql_query = SELECT id, name FROM parent
sql_attr_multi = uint b1 from query; SELECT parent_id, id FROM branch1
sql_attr_multi = uint b2 from query; SELECT parent_id, id FROM branch2
这完全在狮身人面像中进行了“连接”。
然后可以直接在MVA上使用,例如SphinxQL查询
select * from facetIndex facet b1 facet b2
答案 1 :(得分:1)
您可以使用窗口函数仅在第一行进行外部联接:
select t.id, t.b1_name, b2.b2_name
from (
select p.id, b1.b1_name, row_number() over (partition by p.id order by b1_name) as rn
from parent p
join branch_1 b1 on b1.parent_id = p.id
) t
left join (
select parent_id, b2_name,
row_number() over (partition by parent_id) as rn
from branch_2
) b2 on b2.parent_id = t.id and b2.rn = t.rn
order by t.id;