SUB-CONSULTATIONS IN SQL?

Asked

Viewed 83 times

1

Using the BD below, how would return the name of the person who rented the date '2016/11/17'. Name is in the person table and the date in the loan table.

CREATE DATABASE escola;
  USE escola;

CREATE TABLE IF NOT EXISTS acervo (
  id int(11) NOT NULL PRIMARY KEY,
  cod_barra int(15) DEFAULT NULL,
  edicao tinyint(3) DEFAULT NULL,
  categoria_id int(50) DEFAULT NULL,
  genero_id varchar(50) DEFAULT NULL,
  editora_id varchar(50) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS pessoa (
  id int(11) NOT NULL PRIMARY KEY,
  nome varchar(45) DEFAULT NULL,
  endereco varchar(45) DEFAULT NULL,
  num varchar(5) DEFAULT NULL,
  bairro varchar(20) DEFAULT NULL,
  cidade varchar(20) DEFAULT NULL,
  estado varchar(15) DEFAULT NULL,
  cpf varchar(14) DEFAULT NULL,
  aluno tinyint(1) DEFAULT NULL,
  funcionario tinyint(1) DEFAULT NULL,
  professor tinyint(1) DEFAULT NULL,
  data_nac date DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 ;

create table emprestimo(
  id int auto_increment primary key,
  data_do_aluguel date,
  hora time,
  devolvido tinyint(1),
  acervo_id int,
  pessoa_id int,
  foreign key(acervo_id) references acervo(id),
  foreign key(pessoa_id) references pessoa(id)
);

1 answer

3

Would look like this using INNER JOIN to find the person, but will only return the name and date:

SELECT pes.nome as pessoa,
       emp.data_do_aluguel
  FROM emprestimo emp
       INNER JOIN pessoa pes ON pes.id = emp.pessoa_id
 WHERE emp.data_do_aluguel = '2016-11-17'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.