Mysql How to sort row ORDER BY another query?
-
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?
-
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?
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
-
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?
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 aboveselect 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