Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Mysql How to sort row ORDER BY another query?

Mysql How to sort row ORDER BY another query?

Scheduled Pinned Locked Moved Database
databasemysqldata-structurestutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zac Ang
    wrote on last edited by
    #1

    My database tbl_user has the following columns :

    | User\_id | Upline | Right\_id | Left\_id |
    +--------+---------+----------+---------+
    | 1      | 0       |   4      | 7       |
    | 2      | 0       |   6      | 5       |
    | 3      | 0       |   0      | 0       |
    | 4      | 1       |   0      | 0       |
    | 5      | 2       |   0      | 0       |
    | 6      | 2       |   0      | 0       |
    | 7      | 1       |   0      | 0       |
    | 8      | 0       |   0      | 0       |
    | 9      | 0       |   0      | 0       |
    | 10     | 0       |   0      | 0       |  
    +--------+---------+----------+---------+
    
    $user\_id = 2;
    
    $user\_tree = $this->getAllRec("u.user\_id",  
    "tbl\_user u",
    "WHERE u.Upline = '".$user\_id."'
    ORDER BY Right\_id ASC");
    
    print\_r($user\_tree);
    

    I want the array `$user_tree` to hold (user_id 5) first, then (user_id 6), how to do that?

    I want to print **Left_id** first, then only print **Right_id**. May I know how to do it?

    Do I need to use left join or use another query?

    R C 2 Replies Last reply
    0
    • Z Zac Ang

      My database tbl_user has the following columns :

      | User\_id | Upline | Right\_id | Left\_id |
      +--------+---------+----------+---------+
      | 1      | 0       |   4      | 7       |
      | 2      | 0       |   6      | 5       |
      | 3      | 0       |   0      | 0       |
      | 4      | 1       |   0      | 0       |
      | 5      | 2       |   0      | 0       |
      | 6      | 2       |   0      | 0       |
      | 7      | 1       |   0      | 0       |
      | 8      | 0       |   0      | 0       |
      | 9      | 0       |   0      | 0       |
      | 10     | 0       |   0      | 0       |  
      +--------+---------+----------+---------+
      
      $user\_id = 2;
      
      $user\_tree = $this->getAllRec("u.user\_id",  
      "tbl\_user u",
      "WHERE u.Upline = '".$user\_id."'
      ORDER BY Right\_id ASC");
      
      print\_r($user\_tree);
      

      I want the array `$user_tree` to hold (user_id 5) first, then (user_id 6), how to do that?

      I want to print **Left_id** first, then only print **Right_id**. May I know how to do it?

      Do I need to use left join or use another query?

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Zac Ang wrote:

      "WHERE u.Upline = '".$user_id."'

      Your getAllRec method is forcing you to write code which is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query. PHP: SQL Injection - Manual[^] PHP: Prepared statements and stored procedures - Manual[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      1 Reply Last reply
      0
      • Z Zac Ang

        My database tbl_user has the following columns :

        | User\_id | Upline | Right\_id | Left\_id |
        +--------+---------+----------+---------+
        | 1      | 0       |   4      | 7       |
        | 2      | 0       |   6      | 5       |
        | 3      | 0       |   0      | 0       |
        | 4      | 1       |   0      | 0       |
        | 5      | 2       |   0      | 0       |
        | 6      | 2       |   0      | 0       |
        | 7      | 1       |   0      | 0       |
        | 8      | 0       |   0      | 0       |
        | 9      | 0       |   0      | 0       |
        | 10     | 0       |   0      | 0       |  
        +--------+---------+----------+---------+
        
        $user\_id = 2;
        
        $user\_tree = $this->getAllRec("u.user\_id",  
        "tbl\_user u",
        "WHERE u.Upline = '".$user\_id."'
        ORDER BY Right\_id ASC");
        
        print\_r($user\_tree);
        

        I want the array `$user_tree` to hold (user_id 5) first, then (user_id 6), how to do that?

        I want to print **Left_id** first, then only print **Right_id**. May I know how to do it?

        Do I need to use left join or use another query?

        C Offline
        C Offline
        CHill60
        wrote on last edited by
        #3

        Firstly - take note of the previous comment about SQL Injection!

        Quote:

        I want the array `$user_tree` to hold (user_id 5) first, then (user_id 6), how to do that?

        Then you need to order your results by [user_id] e.g.

        select *
        from @demo
        where upline = 2
        order by [User_id];

        Quote:

        I want to print **Left_id** first, then only print **Right_id**. May I know how to do it?

        I'm not entirely sure what you mean here, but it sounds like you want the columns in a different order in your result. In the example above I used select * - that is not actually good practice. It is much better to list the columns that you want, in the order that you want them - this helps protect any code that is using the results from any subsequent changes to the table schema, such as adding a new column. However, if you mean you want to sort by one column within the "grouping" of another column then you simply add a list of things to sort by. This example covers both the scenarios above

        select Left_id,Right_id
        from @demo
        order by Upline, [User_id];

        If you meant something else then reply to this solution and I will try to help

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups