我加入了兩個SELECTS。第一個檢索:MySQL-左連接顯示比預期多的行
t1
id value
1 149
2 149
3 149
4 149
第二個:
t2
id value
149 2
149 13
149 145
149 149
所以,我加入T1 /上t1.value = t2.id
T2。但結果如下:
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
我認爲出現的問題,因爲這是選擇,而不是表。谷歌搜索了很多後,我找不到任何解決方案。
編輯: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
選擇分別檢索Table 1和表2。
請發表您的查詢和表格結構 – FabioCosta