douqie1852 2019-01-18 16:39
浏览 32
已采纳

如何从子表中回拨父母姓名?

I have two tables as follows:

games

id | game_name | console_id
    1     God of War     1
    2     Zelda          3
    3     Sonic          4

consoles

id | console_name 
1    PS4
2    Xbox
3    Switch
4    Mega Drive

I've linked them via the Designer view so when I go to insert a name game (I'm doing this via the phpMyAdmin control panel) as soon as I click on "console_id" it gives me a drop down of 1 - PS4, 2 - Xbox and so on. So the games table can now read from consoles table no problem so I think I've got everything correct from that side of things.

Now I can run this code

SELECT id, game_name, console_id FROM games

and I will get the output

1 - God of war - 1

What I would like to do is something like this:

SELECT id, game_name, console_id.console_name FROM games

So instead of it saying God of war is on console ID 1, it says its on console.name PS4.

How can I do it, as I thought by linking the tables it would allow me to do it.

  • 写回答

1条回答 默认 最新

  • douzhongqiu5032 2019-01-18 18:16
    关注

    You need a JOIN between the tables. In this case, you want to join the consoles table to the games table by matching the id field of the former to the console_id field of the latter, like this:

    SELECT g.id, g.game_name, c.console_name
    FROM games g
    INNER JOIN consoles c
      ON c.id = g.console_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看