I am developing a way to store files using php & mysql. I am hitting a "mind freeze" with this piece of SQL
So, i have this table:
IDIGORDISKFOLDER NAME PARENT
1 |/ |(null)
6 |subfolderl2 |2
3 |/ |(null)
4 |pics |1
5 |/ |0
6 |subfolderl2 |2
7 |subfolderl3 |6
And I am trying to get Something like this: getting parents for id number 7
IDIGORDISKFOLDER NAME PARENT
7 |subfolder13 |6
6 |subfolderl2 |2
6 |subfolderl2 |2
1 |/ |(null)
After some extensive search, I've created the following (SQL) code:
DROP PROCEDURE IF EXISTS getTopParents;
delimiter //
DROP PROCEDURE IF EXISTS getTopParents;
CREATE PROCEDURE getTopParents(in someId int)
BEGIN
declare pid int default NULL;
SELECT parent into pid from igorDiskFolder WHERE idigorDiskFolder = someId;
if (pid is not NULL) then
select idigorDiskFolder, name, parent from igorDiskFolder where idigorDiskFolder = someId;
CALL getTopParents(pid);
end if;
END //
delimiter ;
call getTopParents(2);
But, for some reason, I can't get it running. The SELECT statements itself are working. the "declare" seems to throw an error. I've also tried with SET @pid, but with no luck. If it helps, look at this non-working fiddle: http://sqlfiddle.com/#!2/e3cc6/2
Also, I'd be willing to do this via PHP & MySQL(i), but I don't know if it will affect performance.
A fairly standard approach is using @var
to keep track of parent:
select idigorDiskFolder, name, @id := parent parent
from (select * from igorDiskFolder order by parent desc) a
join (select @id := 7) b
where idigorDiskFolder = @id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.