dongshendi3599 2014-07-24 07:38
浏览 13
已采纳

php请加入帮助

What i am trying to achieve is i have a company table that has 2 userid field(userid and updated userid) i am trying to use left join to get the 2 names from the UserTable here are the structure of my tables

[CompanyTable] 

CompanyID

UserID:int

UpdatedBy:int

[UserTable]


UserID:int

FullName:string

i got 1 working but can't get the second value here are the code i am trying to do please any help would be awesome!!

SELECT company.CompanyID, company.UserID, company.UpdatedBy, users.FullName
    FROM company 
    LEFT JOIN users ON users.UserID = company.UserID
    LEFT JOIN users ON users.UserID = UpdatedBy.UserID
    GROUP BY company.CompanyID
  • 写回答

2条回答 默认 最新

  • dtbl1231 2014-07-24 07:43
    关注

    You can try as below. The Idea is to JOIN the same users table twice with different alias and use them while selecting the fields. You have 2 ids in the company table and you can use the column alias to make it more readable for example u2.FullName as FullName_UpdatedBy means the full name of the updated by user.

    select
    c.CompanyID,
    c.UserID,
    u1.FullName,
    c.UpdatedBy,
    u2.FullName as FullName_UpdatedBy
    from company c
    left join users u1 on u1.UserID = c.UserID
    left join users u2 on u2.UserID = c.UpdatedBy
    group by c.CompanyID
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常