我加入了两个SELECTS。第一个检索:
t1
id value
1 149
2 149
3 149
4 149
第二个:
t2
id value
149 2
149 13
149 145
149 149
所以,我将加入 t1/t2 t1.value = t2.id
。但结果如下:
1 149 2
1 149 13
1 149 145
1 149 149
2 149 2
2 149 13
2 149 145
2 149 149
3 149 2
3 149 13
3 149 145
3 149 149
4 149 2
4 149 13
4 149 145
4 149 149
当所需的结果应如下所示时:
1 149 2
2 149 13
3 149 145
4 149 149
我认为出现问题是因为这是SELECT而不是表。谷歌搜索了很多后,我找不到任何解决方案。
编辑: MySQL 查询:
SELECT t1.id_sequence,t2.id_category, t2.tree
FROM
(
SELECT * FROM (SELECT 1 AS id_sequence UNION SELECT 2 AS id_sequence UNION SELECT 3 AS id_sequence UNION SELECT 4 AS id_sequence ) as tbl1
CROSS JOIN (SELECT DISTINCT id_catalog_category AS 'id_category' from catalog_category where id_catalog_category = 149) as tbl2
) as t1
LEFT JOIN
(
SELECT child.id_catalog_category AS id_category, ancestor.id_catalog_category AS tree
FROM catalog_category AS child
JOIN catalog_category AS ancestor
ON (child.lft BETWEEN ancestor.lft AND ancestor.rgt)
WHERE child.id_catalog_category = 149 AND ancestor.id_catalog_category != 1
) as t2
ON t1.id_category = t2.id_category
分别选择检索到的 table1 和 table2。