Tenho esse modelo relacional simples, e preciso de uma query que seleciona o aluno e o nome do aluno.
Tente da seguinte forma:
SELECT A.matricula_aluno,P.nome FROM aluno AS A,pessoa AS P
-
Show! Muito tempo sem trabalhar com sql! :) Estou baixando um curso aqui para voltar a ativa! – Thiago Cunha 12/05/18 às 22:52
-
1hehe, também estou nessa tava um tempo sem mexer com SQL to voltando a fazer umas query agora. – Bruno Campos 12/05/18 às 23:05
-
Isso está certo ? Funciona ? De qual tabela veio o campo
matricula_aluno
? Esse tipo de consulta está executando umCROSS JOIN
entre as tabelas e não responde a pergunta. Veja só: sqlfiddle.com/#!9/43afd8/20 – Lacobus 13/05/18 às 14:00
Traduzindo seu modelo para MySQL
:
CREATE TABLE pessoa
(
matricula BIGINT NOT NULL,
senha VARCHAR(32),
nome VARCHAR(255),
email VARCHAR(255),
logradouro VARCHAR(255),
bairro BIGINT,
cidade BIGINT,
uf VARCHAR(2),
PRIMARY KEY(matricula)
);
CREATE TABLE aluno
(
matricula BIGINT NOT NULL,
PRIMARY KEY(matricula)
);
CREATE TABLE professor
(
matricula BIGINT NOT NULL,
PRIMARY KEY(matricula)
);
ALTER TABLE aluno ADD CONSTRAINT aluno_matricula_fk FOREIGN KEY (matricula) REFERENCES pessoa (matricula);
ALTER TABLE professor ADD CONSTRAINT professor_matricula_fk FOREIGN KEY (matricula) REFERENCES pessoa (matricula);
Cadastro de Alunos:
INSERT INTO pessoa ( matricula, nome ) VALUES ( 1234, 'JOAO' );
INSERT INTO aluno ( matricula ) VALUES ( 1234 );
INSERT INTO pessoa ( matricula, nome ) VALUES ( 9876, 'MARIA' );
INSERT INTO aluno ( matricula ) VALUES ( 9876 );
INSERT INTO pessoa ( matricula, nome ) VALUES ( 1000, 'JOSE' );
INSERT INTO aluno ( matricula ) VALUES ( 1000 );
INSERT INTO pessoa ( matricula, nome ) VALUES ( 7777, 'JESUS' );
INSERT INTO aluno ( matricula ) VALUES ( 7777 );
Cadastro de Professores:
INSERT INTO pessoa ( matricula, nome ) VALUES ( 2222, 'ALBERT EINSTEIN' );
INSERT INTO professor ( matricula ) VALUES ( 2222 );
INSERT INTO pessoa ( matricula, nome ) VALUES ( 5555, 'ISAAC NEWTON' );
INSERT INTO professor ( matricula ) VALUES ( 5555 );
Consulta de Alunos:
SELECT
p.matricula AS matricula_aluno,
p.nome AS nome_aluno
FROM
aluno AS a
JOIN
pessoa AS p ON ( p.matricula = a.matricula );
Saída:
| matricula_aluno | nome_aluno |
|-----------------|------------|
| 1000 | JOSE |
| 1234 | JOAO |
| 7777 | JESUS |
| 9876 | MARIA |
Consulta de Professores:
SELECT
pes.matricula AS matricula_professor,
pes.nome AS nome_professor
FROM
professor AS prof
JOIN
pessoa AS pes ON ( pes.matricula = prof.matricula );
Saída:
| matricula_professor | nome_professor |
|---------------------|-----------------|
| 2222 | ALBERT EINSTEIN |
| 5555 | ISAAC NEWTON |
Consulta geral de pessoas:
SELECT
pes.matricula AS matricula_pessoa,
pes.nome AS nome_pessoa,
(CASE
WHEN prof.matricula IS NOT NULL THEN 'PROFESSOR'
WHEN a.matricula IS NOT NULL THEN 'ALUNO'
ELSE ''
END) AS tipo_pessoa
FROM
pessoa AS pes
LEFT JOIN
aluno AS a ON ( pes.matricula = a.matricula )
LEFT JOIN
professor AS prof ON ( pes.matricula = prof.matricula )
Saída:
| matricula_pessoa | nome_pessoa | tipo_pessoa |
|------------------|-----------------|-------------|
| 1000 | JOSE | ALUNO |
| 1234 | JOAO | ALUNO |
| 2222 | ALBERT EINSTEIN | PROFESSOR |
| 5555 | ISAAC NEWTON | PROFESSOR |
| 7777 | JESUS | ALUNO |
| 9876 | MARIA | ALUNO |
SQLFiddle: http://sqlfiddle.com/#!9/43afd8/18