简体   繁体   English

从3个表中创建一个选择

[英]Create a select from 3 tables

I´m trying to create a select from 3 different tables. 我正在尝试从3个不同的表中创建一个选择。 I want to show the name of the student, his class and the date of register. 我想显示学生的姓名,他的班级和注册日期。 I have 3 tables "aluno" (student), "turma" (class) and "data..." (date). 我有3个表格“ aluno”(学生),“ turma”(课程)和“ data ...”(日期)。 The name of the student is on the first table named "aluno". 学生的名字在第一个名为“ aluno”的表上。 The class of the stundet is on the table named "turma". 该stundet的类在名为“ turma”的表上。 The register on the class is on the table named "matricula". 该类的寄存器在名为“ matricula”的表上。 My objetive is to show the name of the stundent, the name of the class (designacao) and the date (data). 我的目标是显示笨拙的名称,类的名称(designacao)和日期(数据)。 My function that does this sql command is doing a join, but i´m not sure if its ok.. I think the select is wrong. 我执行此sql命令的函数正在执行联接,但是我不确定它是否可以..我认为选择是错误的。

function DBRead15()

$sql="SELECT aluno.nome as nome, matricula.*, turma.* 
  FROM matricula
  LEFT JOIN aluno ON aluno.n_processo = matricula.n_processo";
$result=DBExecute($sql);

while($res=mysqli_fetch_assoc($result))
{
    $data[]=$res;
}
return $data;

表与数据

表铝

表图尔玛

表矩阵

You started doing the query partially, but can continue as follows: 您开始部分地执行查询,但是可以继续进行以下操作:

SELECT aluno.nome as nome, turma.designacao, matricula.data
FROM aluno
LEFT JOIN turma ON aluno.n_processo = matricula.n_processo
LEFT JOIN matricula ON turma.idturma = matricula.turma

This query assigns the connection between aluno and turma as aluno_id (one-to-many relationship) and the connection between turma and matricula as turma_id (one-to-many relationship). 此查询将aluno和turma之间的连接分配为aluno_id (一对多关系),并将turma和矩阵之间的连接分配为turma_id (一对多关系)。 This query may not work perfectly for you, you will have to change aluno_id to whatever column connects your students to their classes. 该查询可能对您而言并不完美,您必须将aluno_id更改为将您的学生连接到他们的班级的任何列。

This query is an example of how table JOIN in SQL. 此查询是如何在SQL中联接表的示例。

Looking to your tables 看你的桌子

  SELECT aluno.nome as nome, matricula.*, turma.* 
  FROM matricula
  LEFT JOIN aluno ON aluno.n_processo = matricula.n_processo
  LEFT JOIN turma ON matricula.turma = turma.idturma 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM