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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:36:40+00:00 2026-05-21T07:36:40+00:00

I want to build a mySQL query, which returns all nodes in a graph

  • 0

I want to build a mySQL query, which returns all nodes in a graph in x depth from a given node. The depth will be only 2-4.

The table structure is (neighborIDs can contain multiple values):

Id  Name  Desc  neighborIDs

So the task is basically a Breadth-first search in mySQL. I have found a way to do it in T-SQL, is this possible in mySQL?
Is a single SQL query better, than writing a PHP function, that runs a simple SELECT on every neighbour of a node (so basically making tons of simple queries)?

Thanks for help


A try:

SELECT  root.ID,
        d1.ID,
        d2.ID
FROM    Locations root
        LEFT JOIN Locations d1 ON
          root.neighborIDs LIKE CONCAT('%',d1.id,'%')
        LEFT JOIN Locations d2 ON
          d1.neighborIDs LIKE CONCAT('%',d2.id,'%')
WHERE root.id = 1  # i guess this defines the starting node for the search..

An example table is:

id   name   desc                   neighborIDs  
1    id1    --     
2    id2    ---        
3    id3    neighborours are 1,2   1,2  
4    id4    neighbour is 3         3
10   id10   neigh is 4             4

If i run the query with the input id=1, it should return the row with id=3 if the BFS goes 1 level deep.


Another try:

SELECT id,neighborIDs
FROM locations
WHERE id = 3
OR
neighborIDs LIKE '%3%'
OR (SELECT neighborIDs FROM locations WHERE id = 3) LIKE CONCAT('%',id,'%')

This query selects the neighbors of the node with id = 3.

  • 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-21T07:36:40+00:00Added an answer on May 21, 2026 at 7:36 am

    step 0: Create a view that shows all neighbour pairs

    CREATE VIEW neighbour AS
    ( SELECT loc1.id AS a
           , loc2.id AS b
      FROM locations loc1
         , locations loc2
      WHERE FIND_IN_SET(loc1.id, loc2.neighbours)>0
         OR FIND_IN_SET(loc2.id, loc1.neighbours)>0
    ) ;
    

    step 1: Find neighbours of depth 1

    SELECT b AS depth1
    FROM neighbour
    WHERE a = 1;               <-- for root with id=1
    

    step 2: Find neighbours of depth 2

    SELECT DISTINCT d2.b AS depth2
    FROM neighbour d1
      JOIN neighbour d2
        ON d1.b = d2.a
          AND d2.b != 1
    WHERE d1.a = 1                <-- for root with id=1
      AND d2.b NOT IN
         ( SELECT b AS depth1     <- depth1 subquery
           FROM neighbour
           WHERE a = 1            <-- for root with id=1
          )
    ;
    

    step 3: Find neighbours of depth 3

    SELECT d3.b as depth3
    FROM neighbour d1
      JOIN neighbour d2
        ON d1.b = d2.a
        AND d2.b != 1
        AND d2.b NOT IN
           ( SELECT b as depth1
             FROM neighbour
             WHERE a = 1
           )
      JOIN neighbour d3
        ON d2.b = d3.a
        AND d3.b != 1
    WHERE d1.a = 1
      AND d3.b NOT IN
         ( SELECT b as depth1
           FROM neighbour
           WHERE a = 1
          )
      AND d3.b NOT IN
         ( SELECT d2.b AS depth2
           FROM neighbour d1
             JOIN neighbour d2
               ON d1.b = d2.a
               AND d2.b != 1
           WHERE d1.a = 1
             AND d2.b NOT IN
                ( SELECT b AS depth1
                  FROM neighbour
                  WHERE a = 1
                )
         )
    ;
    

    As you can see, the growth is exponential for the number of query lines, so I won’t try the level 4.

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

Sidebar

Related Questions

I want to build a python program that deletes all the photos from my
I want to build a form_tag that will allow me to post a new
I want build a query to return a list of project IDs for the
I am building application which needs to have OOP style MySQL query builder. I
I want to build query using two array 1 for column and another for
i want build a photography app with effects . e.g. old images with brown
I want build a sketch pad app on iPhone, I assume that this type
i want to build a screen for user to get his marks on competitions,
I want to build flash application that can detect the user eyes color and
I want to build a basic Client-Server application, where my android smartphone can stream

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.