duanlian1960 2015-06-13 12:20
浏览 41
已采纳

Php Mysql:通过2个用户ID检索2个用户名

I have one problem that I can't solve. I am doing pair generator for tournament.

I have two tables. The first table contains the usernames and userids. The second table contains match_id, round, user1_id, user2_id

Table 1

user_id  username
21         John
22         Peter
23         Ana
24         Dan


Table 2
match_id  round  user1_id   user2_id
1           1       21          22
2           1       23          24
3           2       21          23
4           2       24          22
.
.
.

And I want to echo

Round 1
John - Peter
Ana - Dan

Round 2
John - Ana
Dan - Peter

Round 3
...

Round 4
...

Can it be done with only one SQL code so I can get both usernames from Table1 for user1_id and user2_id?

To retrieve only one username via user_id I had no problem and its working great:

$sql = "SELECT table1.username, table3.won 
        FROM table1 
        INNER JOIN table3 ON table1.user_id=table3.user_id;";

I know that i could instead of using user_ids in Table2 just use usernames, but I would like to do it this way like I explained above.

  • 写回答

4条回答 默认 最新

  • douchun2158 2015-06-13 13:32
    关注
    select CONCAT((select username from table1 t1 where t.user_id=t1.user_id=t.user_id) ,'-',(select username from table1 t1 where t.user_id=t1.user_id=t.user_id)) from table2 t
    

    OR

     select CONCAT(t1.username,'-',t3.username) from table2 t2
      inner join table1 t1 on t1.user_id=t2.user_id
      inner join table1 t3 on t3.user_id=t2.user_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探