-1

I have table like this

+-------------+-------------+-----------+
| id_struc    | id_emply    | id_boss   |
+-------------+-------------+-----------+
|           1 |       1     |     0     | 
|           3 |       2     |     3     |
|           6 |       4     |     1     |
|           7 |       5     |     1     |
|           9 |       6     |     3     |
|          10 |       3     |     4     |
+-------------+-------------+-----------+

I want to sort it so I can get something like this.

+-------------+-------------+-----------+
| id_struc    | id_emply    | id_boss   |
+-------------+-------------+-----------+
|           1 |       1     |     0     |
|           6 |       4     |     1     |
|           7 |       5     |     1     |
|          10 |       3     |     4     |
|           3 |       2     |     3     |
|           9 |       6     |     3     |
+-------------+-------------+-----------+

It's sort by id_boss. So every id_emply couldn't be at the top of their bosses. Can I possibly do that?

shA.t
  • 16,580
  • 5
  • 54
  • 111
Samadung
  • 19
  • 1
  • 1
  • 7
  • 2
    sorry but you result is not order by id_boss, id_boss:3 is after id_boss:4 – C.Fasolin Aug 19 '17 at 08:49
  • it looks like order by id_emply, id_boss to me? – Learning Aug 19 '17 at 08:52
  • I meant I want to sort it but the employee could be at the top of their bosses – Samadung Aug 19 '17 at 08:53
  • In mysql you do not sort data directly in data tables, you order resultsets of your queries. You are probably looking for a query that would produce such an ordered resultset. – Vojtěch Dohnal Aug 19 '17 at 09:03
  • a good question has some code. –  Aug 19 '17 at 09:04
  • What do you mean by _at the top of their bosses_? Can you please expand/correct your example to make your desired result more clear? As of now I don't see any consistent sorting. – Marvin Aug 19 '17 at 09:04
  • @marvin, I guess user2999081 means hierarchical recursive query. – Dmitry Kurbsky Aug 19 '17 at 09:07
  • Which version of MySql? I'm just asking because 8.0 supports [Recursive Common Table Expressions](https://dev.mysql.com/doc/refman/8.0/en/with.html#common-table-expressions-recursive), otherwise you're looking for something like in this [question](https://stackoverflow.com/questions/20215744/how-to-create-a-mysql-hierarchical-recursive-query) – LukStorms Aug 19 '17 at 09:11
  • why downvote? this is a good question... who ever is downvoter please try to understand the question first. then only downvote – Farsay Aug 19 '17 at 09:37

1 Answers1

0

I think you need a query like this:

select t.*
from t
left join t b on t.id_boss = b.id_emply
order by b.id_boss, t.id_emply;

[SQL Fiddle Demo]

shA.t
  • 16,580
  • 5
  • 54
  • 111