dongxianghui3709 2014-06-19 01:43
浏览 21
已采纳

在sql中从另一个表中的数据中选择3个用户的最佳方法是什么? [关闭]

So I am currently working on a site that contains a loan application, in that loan application there is a primary borrower and then there can be up to 2 co-borrowers. What I've done is created a loanSubmission table that contains a uid1, uid2 and uid3 column which represents the user id and then the number of the user. The id of the user is stored in each one of these columns and the users full information is stored within the users table.

I need to select three users from the data in another table. Is there a way to achieve this using a join or would I have to use sub queries? I've never had to join multiple users based on info from another table so any direction would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • doufu1970 2014-06-19 01:59
    关注

    It's pretty simple, you use aliases to join to the same table multiple times:

    SELECT user1.id, user2.id, user3.id FROM loanSubmission
      INNER JOIN users user1 ON user1.id = loanSubmission.uid1
      INNER JOIN users user2 ON user2.id = loanSubmission.uid2
      INNER JOIN users user3 ON user3.id = loanSubmission.uid3
    

    or depending on if you want blank users(which you probably do)

    SELECT user1.id, user2.id, user3.id FROM loanSubmission
      LEFT OUTER JOIN users user1 ON user1.id = loanSubmission.uid1
      LEFT OUTER JOIN users user2 ON user2.id = loanSubmission.uid2
      LEFT OUTER JOIN users user3 ON user3.id = loanSubmission.uid3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么