MySQL statement to ask for help

how does sql write from the database table to the display of the page?

requirement: the first-level classification needs sorting, the first-level classification shows its subcategories, and the subcategories also need sorting.

Feb.27,2021

use recursion


-- with()
WITH temp(ID,Title,Url,ParentID,SortID)
AS
(
--1:ParentID=0
SELECT ID,Title,Url,ParentID,1 AS Level FROM [TableName]
WHERE ParentID=0
UNION ALL
--2:
--3:
SELECT a.ID,a.Title,a.Url,a.ParentID,b.SortID FROM [TableName] a

INNER JOIN
temp b
ON (a.ParentID=b.ID)--
)
SELECT * FROM temp--4:  
  

" Click here ""

Menu