mysql query unite 2 columns

前端 未结 2 1159
野的像风
野的像风 2021-01-28 04:37

I have a table friends with 4 columns (id, sender, receiver, status) and I need a query that will unite (reunion, I dont know the word in english) the sender and receiver colums

相关标签:
2条回答
  • 2021-01-28 04:49

    This sounds like a job for union:

    select sender as id from <table_name>
    union
    select receiver as id from <table_name>
    

    Note that union will remove duplicates (use union all if you want to keep duplicates).

    0 讨论(0)
  • 2021-01-28 05:04
    SELECT DISTINCT a.iResult
    FROM
        (SELECT sender as iResult FROM tableName
            UNION
         SELECT receiver as iResult FROM tableName) a
    ORDER BY iResult ASC
    

    Returns

    2
    3
    6
    7
    8
    9
    10
    

    OR

    SELECT GROUP_CONCAT(b.iResult)
         (SELECT DISTINCT a.iResult
         FROM
                 (SELECT sender as iResult FROM tableName
                     UNION
                  SELECT receiver as iResult FROM tableName) a
             ORDER BY iResult ASC) b
    

    returns

    2,3,6,7,8,9,10
    
    0 讨论(0)
提交回复
热议问题