Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择表中的所有数据,同时用另一个表中的值替换列?_Php_Mysql - Fatal编程技术网

Php 选择表中的所有数据,同时用另一个表中的值替换列?

Php 选择表中的所有数据,同时用另一个表中的值替换列?,php,mysql,Php,Mysql,目前,我有两个表,约会和用户: appointment_id | time | teacher_id | student_id 1 | 11 | 2 | 1 2 | 12 | 2 | 1 user_id | name 1 | student 2 | teacher 预约|时间|教师|学生|学生 1 | 11 | 2

目前,我有两个表,约会和用户:

appointment_id | time | teacher_id | student_id 1 | 11 | 2 | 1 2 | 12 | 2 | 1 user_id | name 1 | student 2 | teacher 预约|时间|教师|学生|学生 1 | 11 | 2 | 1 2 | 12 | 2 | 1 用户名 1 |学生 2 |教师 预期结果:

appointment_id | time | teachername | studentname 1 | 11 | teacher | student 2 | 12 | teacher | student 预约|时间|教师姓名|学生姓名 1 | 11 |教师|学生 2 | 12 |教师|学生 基本上,我希望从约会表中执行SELECT*操作,但我不希望查询给我id,而是希望用名称替换它们。我试着创建一个临时表和一个连接,但没有找到答案。任何帮助都会很好,谢谢

这个怎么样

SELECT
    appointment_id,
    time,
    u.name AS teachername,
    v.name AS studentname
FROM
    appointments AS a
LEFT JOIN
    users AS u ON
    a.teacher_id = u.user_id
LEFT JOIN
    users AS v ON
    a.student_id = v.user_id
你可以签入这个

这个怎么样

SELECT
    appointment_id,
    time,
    u.name AS teachername,
    v.name AS studentname
FROM
    appointments AS a
LEFT JOIN
    users AS u ON
    a.teacher_id = u.user_id
LEFT JOIN
    users AS v ON
    a.student_id = v.user_id
您可以签入这个

试试这个

   select a.appointment_id , a.time ,b.name as teachername ,
          c.name as studentname
   from appointments a 
   inner join users b
   on a.teacher_id = b.user_id
   inner join users c
   on a.student_id = c.user_id

输出:

   APPOINTMENT_ID   TIME    TEACHERNAME     STUDENTNAME
          1          11     teacher         student
          2          12     teacher         student
试试这个

   select a.appointment_id , a.time ,b.name as teachername ,
          c.name as studentname
   from appointments a 
   inner join users b
   on a.teacher_id = b.user_id
   inner join users c
   on a.student_id = c.user_id

输出:

   APPOINTMENT_ID   TIME    TEACHERNAME     STUDENTNAME
          1          11     teacher         student
          2          12     teacher         student

. . 你试过什么?这是一个非常基本的SQL查询,所以如果您有编写查询的经验,应该不难理解。你试过什么?这是一个非常基本的SQL查询,因此如果您有编写查询的经验,应该不难理解。我想您希望
u.name作为teachername,v.name作为studentname
我想您希望
u.name作为teachername,v.name作为studentname