我有一个项目表(project)和一个用户表(user)
projectproject_id   first_person   second_person   third_person
33             12               78              65user
user_id    user_name
  12          组长
  78          处长
  65          主任
我想查询project表里project_id=33的first_person,second_person,third_person的名字(是user_name,不是user_id),请问用一个sql语句怎样写出来。谢谢了。

解决方案 »

  1.   

    select (select user_name from [user] where user_id=p.first_person) as first_person,
    (select user_name from [user] where user_id=p.second_person) as second_person,
    (select user_name from [user] where user_id=p.third_person) as third_person
    from project p
    where project_id=33
      

  2.   

    select a.project_id ,
           first_person = (select user_name from user b where b.user_id = a.first_person),
           second_person = (select user_name from user b where b.user_id = a.second_person),
           third_person = (select user_name from user b where b.user_id = a.third_person)
    from project a
    where a.project_id=33
      

  3.   

    select a.project_id,
           b.user_name,
           c.user_name,
           d.user_name
    from project a,user b,user c,user d
    where a.first_person = b.user_id
      and a.second_person = c.user_id
      and a.third_person = d.user_id
      

  4.   

    select a.project_id,
           b.user_name,
           c.user_name,
           d.user_name
    from project a,user b,user c,user d
    where a.first_person = b.user_id
      and a.second_person = c.user_id
      and a.third_person = d.user_id
      

  5.   

    --这几个关键字要加[]
    select a.project_id ,
           first_person = (select [user_name] from [user] b where b.[user_id] = a.first_person),
           second_person = (select [user_name] from [user] b where b.[user_id] = a.second_person),
           third_person = (select [user_name] from [user] b where b.[user_id] = a.third_person)
    from project a
    where a.project_id=33
      

  6.   

    各位大哥,如果我想用hibernate,hql语句该怎么写啊。