GarethPoole
GarethPoole

Reputation: 23

MySQL Query to retrieve full URL slug

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!

Upvotes: 1

Views: 936

Answers (2)

Femaref
Femaref

Reputation: 61437

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

Upvotes: 1

Claude Vedovini
Claude Vedovini

Reputation: 2481

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

Upvotes: 0

Related Questions