dourong4031 2015-10-07 14:27
浏览 148
已采纳

MySQL表连接:一个记录,两个字段,另一个表中的id

I'd like to join two tables, producing a result where the IDs in the 2nd and 3rd columns of table2 are replaced by the corresponding name values from table1:

table1:
  id - name
  ---------
  1 -  Sam
  2 -  Ben
  3 -  Nick

table2: 
id      name1           name2        upvote   downvote
1 1(id of Ben) 3(id of Nick) 150 8

So my goal here is to join these tables and produce:

1 -  Ben  -  Nick  -  150   - 8

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • dongye9820 2015-10-07 14:43
    关注

    A simple Join is needed, See more : MySQL Joins Manual

    SELECT 
    table1.id,table1.name,
    table2.name2,table2.upvote,
    table2.downvote 
    FROM `table1`
    INNER JOIN `table2`
    ON
    table2.name2 = table1.id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题