Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:06:11+00:00 2026-06-13T20:06:11+00:00

Say I have this: page_url | canvas_url ————————————————————— http://www.google.com/ | http://www.google.com/barfoobaz http://www.google.com/foo/bar | http://www.google.com/foo

  • 0

Say I have this:

page_url                      | canvas_url
---------------------------------------------------------------
http://www.google.com/        | http://www.google.com/barfoobaz
http://www.google.com/foo/bar | http://www.google.com/foo

I’d like to find the row that is the start of my string ordered by the longest match. The problem I’m facing is finding the longest matching string, rather than just the row that matches that also has a matching one. I.e.

http://www.google.com/foo matches page_url in row 1 and canvas_url in row 2, but if it’s length of both columns rather than a match it would think row 1 is the better match as canvas_url in row 1 is longer.

I could grab all matches and then filter the length in code doing something like:

SELECT *, LENGTH(canvas_url), LENGTH(page_url)
FROM app 
WHERE
    'http://www.google.com/foo' LIKE CONCAT(canvas_url, '%') OR
    'http://www.google.com/foo' LIKE CONCAT(page_url, '%')

Or perform 2 subselections grabbing the top matches of canvas_url respective page_url and then filter that to 1 in code, but I would prefer (barring any ridiculous performance issues) having the database just return what I need.

My immediate concern is MySQL but I need to target SQLite and Postgress, so I’d be happy with an answer in either of those.

Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:06:12+00:00Added an answer on June 13, 2026 at 8:06 pm

    This will work to get the longest actual match length (not just the longest url in the record):

    -- Get page_url matches
    SELECT *, LENGTH(page_url) AS MatchLen
    FROM app 
    WHERE 'http://www.google.com/foo' LIKE CONCAT(page_url, '%') -- can't tell from question if this should be reversed
    UNION ALL
    -- Get canvas_url matches
    SELECT *, LENGTH(canvas_url) AS MatchLen
    FROM app 
    WHERE 'http://www.google.com/foo' LIKE CONCAT(canvas_url, '%')
    -- Bring the longest matches to the top
    ORDER BY MatchLen DESC -- May need to add a tie-breaker here
    LIMIT 1
    

    Here is a running example on SqlFiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say i have this link: http://www.domain.com/content/view/1/seo-url now, lets say a user misspeled the
Let us say i have a page http://www.abc.com/xyz.html and i am going to access
Say I have a web page like this on an Ubuntu 12.04 web server
I have this simple nodejs page http://pastebin.com/HxMsHBbg /** * Module dependencies. */ var express
let say I have this url: http://myweb/item/info/1 with a link to http://myweb/item/edit/1 and I
I'm confused about MVC routes. Let's say I have this domain: poopoopants.com. I want
Say my URL address is www.example.com I want to have an if statement in
Say I have this code in my page: <script language=javascript> $(document).ready(function() { $(.test).click(function() {
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.