douxueke5653 2013-09-08 17:05
浏览 19
已采纳

使用另一个表中的一列作为我当前表MySQL的参考

I have 2 tables. First table (user) containing 3 columns:

| user_id | username | password |
---------------------------------
|  253    | John     | 12345678 |
|  180    | Katrina  | jellyfis |

Second table (quiz score) containing 3 columns:

| score   |   rank   | answered |
---------------------------------
|  6520   |   56     |   51     |
|  9846   |   2      |   102    |

In this particular scenario, John has a score of 6520 points while Katrina has a score of 9846. Given that the user_id is in one table whilst score is in another table, is there a specific way I can establish a connection between these 2 columns?

Say for example, I've created a simple profile display in PHP where details about the user (score, rank and answer) are being shown. I want to display the score of that user while both of those columns are in different tables.

Hope I make myself clear enough. For those who want the reason I'm doing this; it's because I'm working on a project that involves using 2 different tables based on the user's profile and the user's score.

  • 写回答

1条回答 默认 最新

  • dousuo2812 2013-09-08 17:16
    关注

    Add user_id column to the second table.

    You will have to add FOREIGN KEY (user_id) REFERENCES firstTableName(user_id) in your create table statement. Please refer manual for more details.

    Your second table will look like

    | user_id | score   |   rank   | answered |
    -------------------------------------------
    |  253    |  6520   |   56     |   51     |
    |  180    |  9846   |   2      |   102    |
    

    You can then join the two tables on user id column to get the details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度