mysql在多个记录中的select查询中通过id获取名称

时间:2016-07-06 12:31:15

标签: mysql

我陷入了mysql查询。我有一个员工表,其字段是id,name,bossid和bossid是员工的父ID。

enter image description here

我如何编写查询以此格式打印数据

 id   name     boss
 1    rajesh   rajesh
 2    vijay    rajesh
 3    ravi     vijay

2 个答案:

答案 0 :(得分:2)

def my_calc(my_series, func=max):
   if not func in {'min', 'max'}:
      raise ValueError('{} is not a valid method name!'.format(func))
   return getattr(my_series.rolling(...), func)()

foo = my_calc(some_series, 'min')
bar = my_calc(some_series, 'max')

试试这个http://sqlfiddle.com/#!9/ddb50/2

答案 1 :(得分:1)

只需加入桌子:

Select t1.id as id 
     , t1.name as Name 
     , t2.name as boss 
  from employee t1 
  join employee t2 
    on t1.bossid = t2.id