Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select pair of rows that obey a rule

Tags:

sql

I have a big table (1M rows) with the following columns: source, dest, distance. Each row defines a link (from A to B).

I need to find the distances between a pair using anoter node. An example: If want to find the distance between A and B, If I find a node x and have: x -> A x -> B I can add these distances and have the distance beetween A and B. My question: How can I find all the nodes (such as x) and get their distances to (A and B)? My purpose is to select the min value of distance.

P.s: A and B are just one connection (I need to do it for 100K connections). Thanks !

like image 339
Lior A Avatar asked Oct 23 '11 19:10

Lior A


People also ask

How do I select specific rows in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do I display a row in SQL?

You can just do Select * from table. It will select entire data from your table.


1 Answers

As Andomar said, you'll need the Dijkstra's algorithm, here's a link to that algorithm in T-SQL: T-SQL Dijkstra's Algorithm

like image 196
wm.wragg Avatar answered Oct 29 '22 23:10

wm.wragg