I have a MySQL table which has the following structure:
mysql> select id,channel,Stats,Bridged from channels; +----------+---------------------------------+---------+-------------------------------+ | id | channel | Stats | Bridged | +----------+---------------------------------+---------+-------------------------------+ | 21523318 | SIP/5602291658-0007f140 | Up | SIP/X.Y.Z.Q-0007f13f > | | 21523321 | SIP/X.Y.Z.Q-0007f13f | Up | SIP/5602291658-0007f140 | | 21523322 | SIP/5154642553-0007f13a | Up | SIP/402-0007f135 | | 21523323 | SIP/402-0007f135 | Up | SIP/5154642553-0007f13a |
Look at each two records, they are paired: for example ID 21523318 has Channel identical with Bridged column from next ID.
My question is: taking into account that the table usually holds several thousands entries, how could I select a single line for each two records?
I mean — once I have the first line and I can verify that the “Bridged” column info exists as “channel” in the db, to display a single line. I can do this recursively (checking for each line if there’s another Bridged channel , but for several thousands it becomes very slow and there are a lot of queries.
I was thinking of doing inner join with the same table on channel=Bridged and then somehow do a group by or something…
Any clues or ideas on how I should proceed?
If channel and bridged are always swapped, this should work for you: