mysql: how to save ORDER BY after LEFT JOIN without reorder?

前端 未结 4 862
你的背包
你的背包 2021-01-22 22:22

I\'ve two table:

1) profiles

+----+---------+
| id | name    |
+----+---------+
|  1 | WILLIAM |
|  2 | JOHN    |
|  3 | ROBERT  |
|  4 | MI         


        
4条回答
  •  梦毁少年i
    2021-01-22 22:58

    SELECT * 
      FROM profiles p 
      LEFT 
      JOIN request_for_friendship r 
        ON (r.from_id = p.id AND r.to_id = 1) 
        OR (r.from_id = 1 AND r.to_id = p.id)  
     ORDER 
        BY name;
    +----+---------+------+---------+-------+
    | id | name    | id   | from_id | to_id |
    +----+---------+------+---------+-------+
    |  8 | CHARLES |    3 |       1 |     8 |
    |  6 | DAVID   | NULL |    NULL |  NULL |
    |  5 | JAMES   | NULL |    NULL |  NULL |
    |  2 | JOHN    |    1 |       1 |     2 |
    |  9 | JOSEPH  |    6 |       9 |     1 |
    |  4 | MICHAEL |    5 |       4 |     1 |
    |  7 | RICHARD | NULL |    NULL |  NULL |
    |  3 | ROBERT  |    2 |       1 |     3 |
    | 10 | THOMAS  | NULL |    NULL |  NULL |
    |  1 | WILLIAM | NULL |    NULL |  NULL |
    +----+---------+------+---------+-------+
    10 rows in set (0.02 sec)
    
    mysql>
    

提交回复
热议问题