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 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图