duanjunao9348 2014-11-10 04:25
浏览 124

如何从两个表中获取数据 - PHP和MySQL

I have two tables in MySQL as below:

Table1 as below:

ID (Primary Key Auto Increment) | Student Name | Age

Table2 as below:

Teacher Name | Student ID (This is the Student ID from table1) | Email | Education

Now I want to get data from both the tables table1 and table2 in a single query.

I want to retrieve the values from both tables where Student ID is equals to something.

Student Name | Age | Teacher Name

Can you please let me know how can I query values from two tables as mentioned above. I have read many tutorials but I can not retrieve it properly. I am new to MySQL so please explain clearly. What should I use table join, union, inner or outer join?

  • 写回答

3条回答 默认 最新

  • duangu3620 2014-11-10 04:29
    关注

    Try this tutorial here.

    You should implement relation by using foreign keys in your tables.

    UPDATED You query should be something like this:

    SELECT t1.StudentName, t1.Age, t2.TeacherName
    FROM table1 t1, table2 t2
    WHERE t1.id = t2.studentid;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘