1

I have a database table that contains URLs in a column. I want to show certain data depending on what page the user is on, defaulting to a 'parent' page if not a direct match. How can I find the columns where the value is part of the submitted URL?

Eg. I have www.example.com/foo/bar/baz/here.html; I would expect to see (after sorting on length of column value):

  • www.example.com/foo/bar/baz/here.html
  • www.example.com/foo/bar/baz
  • www.example.com/foo/bar
  • www.example.com/foo
  • www.example.com

if all those URLs are in the table of course.

Is there a built in function or would I need to create a procedure? Googling kept getting me to LIKE and REGEXP, which is not what I need. I figured that a single query would be much more efficient than chopping the URL and making multiple queries (the URLs could potentially contain many path components).

1
  • Some PL/SQL procedure, for sure. But it is easy to do Sep 6, 2013 at 18:12

1 Answer 1

1

Simple turn around the "Like" operator:

SELECT * FROM urls WHERE "www.example.com/foo/bar/baz/here.html" LIKE CONCAT(url, "%");

http://sqlfiddle.com/#!2/ef6ee/1

1
  • Wow, head slap! I assume if I wanted to allow any protocol I would just concat an additional '%' before url, yes? Although that may catch some unintended. Sep 6, 2013 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.