Encrypt first search query in MySQL?

I want to build a mySQL query that returns all nodes in a graph at depth x from the given node. Depth will be only 2-4.

Table structure (neighboring identifiers may contain several values):

Id  Name  Desc  neighborIDs

Thus, the task is mainly to search by the Breadth method in mySQL. I found a way to do this in T-SQL , is this possible in mySQL? Is a single SQL query better than writing a PHP function that runs a simple SELECT for each node neighbor (so it basically makes a lot of simple queries)?

thanks for the help


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..

Example table:

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 a query with input id id = 1, it should return a line with id = 3 if BFS goes 1 level deep.


Another attempt:

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 neighbors from node with id = 3.

+3
2

0: ,

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
) ;

1: 1

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

2: 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
      )
;

3: 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
            )
     )
;

, , 4.

+1

, . - . (, ).

SELECT  root.ID,
        d1.ID,
        d2.ID
FROM    Locations root
        LEFT JOIN Locations d1 ON
          root.Neighbours LIKE '%'+CAST(d1.ID as varchar)+'%'  --Or equivalent mysql pattern matching function
        LEFT JOIN Locations d2 ON
          d1.Neighbours LIKE '%'+CAST(d2.ID as varchar)+'%'

:

+2

All Articles