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.
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
) ;
SELECT b AS depth1
FROM neighbour
WHERE a = 1; <-- for root with id=1
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
)
;
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.
As mentioned in my comment, you've made your life difficult. But something similar to the following will produce a list of neighbour IDs at each depth. Depending on your exact needs, the result set can be used a subquery and manipulated further to necessary (such as retrieving the names of the neighbours).
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)+'%'
EDIT: Changed INNER JOIN to LEFT JOIN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With