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 6839341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:42:26+00:00 2026-05-26T23:42:26+00:00

I have a database setup as follows (simplified): +———-+———+———-+ | path | val1 |

  • 0

I have a database setup as follows (simplified):

+----------+---------+----------+
|   path   |  val1   |   val2   |
+----------+---------+----------+
|    a/    |   two   |   cow    |
+----------+---------+----------+
|   a/b/   |   one   |   cat    |
+----------+---------+----------+
|  a/b/c   |   NULL  |   bat    |
+----------+---------+----------+

path is the primary key (in actual use, more like Smith/John/Mark/Jr).

PHP provides a path $mypath = "a/b/c";

Is there a way to build a result set of non null values, choosing from all rows whose path is part of $mypath.
That is, to search for the key that matches most exactly, find all non-null values in it, and if there are any nulls, to get them from the second-“best” key.

Eg. Mysql should check a/b/c and return a val2 = “bat”, but see that val1 is set to NULL.
It should therefore check a/b/ and set val1 = “one”;
The final result should be array('val1' => "one", 'val2' => "bat").

A friend told me there is a way with “LOCATE”, but I couldn’t work it out.
Is this doable?

And can someone who knows MySQL help me rewrite the title of this question to be more descriptive?

Thanks.

  • 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-05-26T23:42:27+00:00Added an answer on May 26, 2026 at 11:42 pm

    In conclusion:
    It is probably impossible to do as requested using only SQL.
    However, SQL can get an multidimensional array from the matching keys, and PHP can whittle it down to one array.

    If anyone can improve this to only use SQL (as in the question posted), it would be much appreciated.

    SQL:

    SELECT * FROM `mytable` WHERE `path` = '$mypath' 
    OR (LOCATE(`path`, '$mypath') = 1 AND RIGHT(`path`, 1) = '/') 
    ORDER by length(`page`) desc;
    

    PHP:

    $results = mysql_query(... sql ...);
    $pg = array();
    foreach(array_keys($results[0]) as $key)
        foreach($results as $result)
            if ($result[$key]){ $pg[$key] = $result[$key]; continue 2; }
    

    You can see this in practice at http://codepad.org/UnyEZJ6a

    Notes:

    • locate (used by Outis), and instr (used Nonym) are the same, except for that the order of arguments is reversed.
    • right() is used in the query to ensure that the path is split only on the forward slash.
    • The parenthesis used in the OR is technically redundant, as AND is evaluated before OR.
    • If there is a solution in sql, I have been told it would use the group_by command.

    Hope this can help someone else!

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

Sidebar

Related Questions

I am using ruby on rails and have my database setup as follows in
We have SQL Server database setup. We are setting up a replication scenarios where
If you've got a database setup in Django, how can you have the TestRunner
I have setup multiple SQL Service Broker Queues in a database but have not
I have a tomcat instance setup but the database connection I have configured in
I have a web-app-database 3 tier server setup. Web requests data from app, and
I have a tool that is setup to query our Clearquest Database to return
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
currently I'm trying to have a setup where a generic database is distributed to
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.

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.