tags:

views:

24

answers:

2

First question!

I have a MySQL table which stores all content on the various pages of a site. Let's say there's three fields, id(int), permalink(varchar) and parent(int)(. parent is the id of it's parent page.

I need a query that will build a full URL of a page, I'm guessing using CONCAT. I have it working fine for two levels, but can't figure out a way to make it scale for multiple levels; /root/level1/level2/ etc.

Here's what I have so far.

SELECT 
CONCAT(
(SELECT permalink FROM content WHERE id = 2 LIMIT 1), # id = parent
"/", 
(SELECT permalink FROM content WHERE id = 11 LIMIT 1)) as full_url

Any help, greatly appreciated!

A: 

You cannot do recursion in a query, you would have to use stored procedures but this is not available in MySQL.

Claude Vedovini
+1  A: 

That would be a recursive query, you have to use a stored procedure on the server (Which are avaiable in MySql @Claude).

Femaref
perfect, I'll look into it.
GarethPoole