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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:28:14+00:00 2026-06-06T12:28:14+00:00

I have table with two columns (varchar from, varchar to). This table represents connections

  • 0

I have table with two columns (varchar from, varchar to). This table represents connections betwen nodes (from node, to node). I want to get all nodes connected from or to node that I specify and nodes connected from or to those nodes. Currently I use query below that gives me proper results but I’m searching for neater solution.

//currently used query specified node "node1"
SELECT tonode as node
FROM conn
WHERE
fromnode
IN
(SELECT tonode as node FROM conn WHERE fromnode="node1"
UNION
SELECT fromnode as node FROM conn WHERE tonode="node1")
UNION
SELECT fromnode as node
FROM conn
WHERE
tonode
IN
(SELECT tonode as node FROM conn WHERE fromnode="node1"
UNION
SELECT fromnode as node FROM conn WHERE tonode="node1")


//create table for conn table
CREATE TABLE `conn` (
  `fromnode` varchar(70) NOT NULL,
  `tonode` varchar(70) NOT NULL,
  PRIMARY KEY (`fromnode`,`tonode`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO `conn` (`fromnode`,`tonode`) VALUES
 ('node1','node2'),
 ('node1','node3'),
 ('node3','node2'),
 ('node4','node1'),
 ('node4','node2'),
 ('node4','node5'),
 ('node5','node6'),
 ('node4','node3');
  • 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-06-06T12:28:15+00:00Added an answer on June 6, 2026 at 12:28 pm

    My optimized version:

    SET @origin = "node1";
    SELECT DISTINCT
     IF(c1.fromnode = @origin,
       IF(c1.tonode = c2.tonode,
         IF(c2.fromnode = @origin, c2.tonode, c2.fromnode),
         IF(c2.tonode = @origin, c2.fromnode, c2.tonode)
       ),
       IF(c1.fromnode = c2.tonode,
         IF(c2.fromnode = @origin, c2.tonode, c2.fromnode),
         IF(c2.tonode = @origin, c2.fromnode, c2.tonode)
       )
     ) AS node
    FROM conn AS c1
    LEFT JOIN conn AS c2 ON (c1.fromnode = c2.fromnode OR c1.tonode = c2.fromnode OR c1.fromnode = c2.tonode OR c1.tonode = c2.tonode)
    WHERE c1.fromnode = @origin OR c1.tonode = @origin;
    

    the DESCRIBE output of your old query:

    +----+--------------------+------------+--------+---------------+---------+---------+------------+------+--------------------------+
    | id | select_type        | table      | type   | possible_keys | key     | key_len | ref        | rows | Extra                    |
    +----+--------------------+------------+--------+---------------+---------+---------+------------+------+--------------------------+
    |  1 | PRIMARY            | conn       | index  | NULL          | PRIMARY | 424     | NULL       |    8 | Using where; Using index |
    |  2 | DEPENDENT SUBQUERY | conn       | eq_ref | PRIMARY       | PRIMARY | 424     | const,func |    1 | Using where; Using index |
    |  3 | DEPENDENT UNION    | conn       | eq_ref | PRIMARY       | PRIMARY | 424     | func,const |    1 | Using where; Using index |
    | NULL | UNION RESULT       | <union2,3> | ALL    | NULL          | NULL    | NULL    | NULL       | NULL |                          |
    |  4 | UNION              | conn       | index  | NULL          | PRIMARY | 424     | NULL       |    8 | Using where; Using index |
    |  5 | DEPENDENT SUBQUERY | conn       | eq_ref | PRIMARY       | PRIMARY | 424     | const,func |    1 | Using where; Using index |
    |  6 | DEPENDENT UNION    | conn       | eq_ref | PRIMARY       | PRIMARY | 424     | func,const |    1 | Using where; Using index |
    | NULL | UNION RESULT       | <union5,6> | ALL    | NULL          | NULL    | NULL    | NULL       | NULL |                          |
    | NULL | UNION RESULT       | <union1,4> | ALL    | NULL          | NULL    | NULL    | NULL       | NULL |                          |
    +----+--------------------+------------+--------+---------------+---------+---------+------------+------+--------------------------+
    

    the DESCRIBE output of my query:

    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------------------------------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                                     |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------------------------------------+
    |  1 | SIMPLE      | c1    | index | PRIMARY       | PRIMARY | 424     | NULL |    8 | Using where; Using index; Using temporary |
    |  1 | SIMPLE      | c2    | index | PRIMARY       | PRIMARY | 424     | NULL |    8 | Using index                               |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------------------------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two columns in a SQL table, fooId (int) and fooName (varchar). Is
I have a table containing two columns in SQL that I want to extract
I have an table (in ORADB) containing two columns: VARCHAR unique key and NUMBER
I have this table in a mysql database: mysql> show columns from wifi_network_config_auth_algorithm; +------------------------+-------------+------+-----+---------+-------+
I have table with two columns (startdate & enddate) and I have two submission
I have a table with two columns: person_id person_id with whom 1st field id
I have a table with two columns and one row, and 100% width across
I have a table with two columns: column A column B 1 2 1
I have a table with two columns, GroupId and ParentId (both are GUIDS). The
I have a table with a few thousand rows. The table contains two columns,

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.