我試圖用這種方式顯示產品類別;如何將「WITH」子句MS SQL轉換爲MySQL
Table Name: PSCategories
PSId | PSName | ParentId(FK to PSCategories(PSId))
1 | test1 | NULL
2 | test2 | 1
3 | test3 | NULL
4 | test4 | 3
5 | test5 | 4
我想用這種方式
PSId | PSName | ParentId
1 | test1 | 0
2 | test1 > test2 | 1
3 | test3 | 0
4 | test3 > test4 | 3
5 | test3 > test4 > test5 | 4
這MSSQL查詢可以做它來過濾該表數據,但如何將它轉換到mysql
WITH categories (PSId, PSName, ParentId)
AS (SELECT PSId, CAST(PSName AS VARCHAR(255)), 0
FROM PSCategories AS pm
WHERE pm.ParentId IS NULL
UNION ALL
SELECT ps.PSId, CAST(m.PSName + ' > ' + ps.PSName AS VARCHAR(255)), m.PSId
FROM PSCategories AS ps
JOIN categories AS m
ON m.PSId = ps.ParentId
WHERE ps.ParentId IS NOT NULL
)
SELECT * FROM categories
ORDER BY PSId
將其複製並粘貼爲子查詢... –
這是一個遞歸CTE,c&p不會在這裏工作 –
@MartinK .:遞歸在哪裏? – gexicide