mysql移動列列 不是在mysql中向左移動的列

2021-10-20 23:26:25 字數 3013 閱讀 8338

我正在研究

mysql資料庫.

我有一張**,這張表包含

employeeid和reportingemployeeid列

select e3.`reportingemployeeid` as level0,e2.`reportingemployeeid` as level1,e1.`reportingemployeeid` as level2,e1.`employeeid` as level3

from empreporting e1

left join empreporting e2 on e1.`reportingemployeeid` = e2.`employeeid`

left join empreporting e3 on e2.`reportingemployeeid` = e3.`employeeid`

以上查詢給出以下結果:

level0 level1 level2 level3

\n \n 379 369

\n 379 484 372

\n \n \n 379

所需的結果格式為:

level0 level1 level2 level3

379 369 \n \n

379 484 372 \n

379 \n \n \n

請任何人幫助我.

提前致謝

普拉卡什

最佳答案 最後寫的查詢

select

case

when e3.`reportingemployeeid` is not null then e3.`reportingemployeeid`+1000000

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is not null then e2.`reportingemployeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is not null then e1.`reportingemployeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is null and e1.`employeeid` is not null then e1.`employeeid`

end as level0,

case

when e3.`reportingemployeeid` is not null then e2.`reportingemployeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is not null then e1.`reportingemployeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is not null then e1.`employeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is null and e1.`employeeid` is not null then null

end as level1,

case

when e3.`reportingemployeeid` is not null then e1.`reportingemployeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is not null then e1.`employeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is not null then null

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is null and e1.`employeeid` is not null then null

end as level2,

case

when e3.`reportingemployeeid` is not null then e1.`employeeid`

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is not null then null

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is not null then null

when e3.`reportingemployeeid` is null and e2.`reportingemployeeid` is null and e1.`reportingemployeeid` is null and e1.`employeeid` is not null then null

end as level3

from empreporting e1

left join empreporting e2 on e1.`reportingemployeeid` = e2.`employeeid`

left join empreporting e3 on e2.`reportingemployeeid` = e3.`employeeid`

mysql 移動列的順序

mysql資料庫工具好像沒有調表列順序的功能,但是我們可以通過sql語句實現。語句 quote alter table 表名 modify 欄位名 字段型別 after 字段 quote 舉例 1 把user name列移動到password後面 alter table employee modif...

MySQL 新增列,修改列,刪除列

alter table 新增,修改,刪除表的列,約束等表的定義。sp rename sqlserver 內建的儲存過程,用與修改表的定義。mysql 檢視約束,新增約束,刪除約束 新增列,修改列,刪除列 sp rename sqlserver 內建的儲存過程,用與修改表的定義。先刪除主鍵 alter...

mysql 新增列,修改列,刪除列

原文 示例 alter table tb financial modify create time datetime 3 default null comment 錄入時間 alter table 新增,修改,刪除表的列,約束等表的定義。sp rename sqlserver 內建的儲存過程,用與修...