Answer the question
In order to leave comments, you need to log in
How to compose a Mysql query without using WITH?
There is a plate | id | email | partner_id |
partner_id -> this is a connection with a record from the same table.
The task is to find out all the lower "partners", while getting the level of depth at which they are.
MySql Doesn't support WITH, How to rewrite a query for MYSQL?
WITH list AS
(SELECT upper.id, upper.email, upper.partner_id,
1 AS level
FROM users AS upper
WHERE upper.partner_id IS NULL
UNION ALL
SELECT lower.id, lower.email lower.partner_id,
el.level + 1
FROM users AS lower
INNER JOIN list AS el
ON lower.partner_id = el.id
WHERE lower.partner_id IS NOT NULL)
SELECT *
FROM list;
Answer the question
In order to leave comments, you need to log in
with works recursively - doing thousands of queries is very inefficient.
It is better to change the structure of the table. Google "storing a tree in a database"
Or at least make one complete select and then build an array-tree on your PL (for example, PHP).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question