0
I’m having a hard time getting a SELECT. I need to list all the information(dcodigo, dnome) of the department (DEPT) that have a manager(position). My code is like this:
create database bd_emp;
use bd_emp;
create table DEPT(
dcodigo int unsigned primary key not null
auto_increment,
dnome varchar(36) not null
);
create table EMPR(
matr int unsigned key not null
auto_increment,
nome varchar(12) not null,
sobrenome varchar(15) not null,
dept int unsigned,
fone varchar(14),
dinadim date,
cargo varchar(10),
niveled decimal,
sexo char(1),
datanas date,
salario decimal(9.2),
bonus decimal(9.2),
comis decimal(9.2),
foreign key(dept) references DEPT(dcodigo)
);
create table PROJETOS(
pcodigo int unsigned not null primary key,
pnome varchar(24) not null,
dcodigo int unsigned not null,
resp int unsigned not null,
equipe int unsigned,
dataini date,
datafim date,
psuper varchar(6),
foreign key(dcodigo) references DEPT(dcodigo),
foreign key(resp) references EMPR(matr)
);
insert into DEPT values
(null, 'informatica'),
(null, 'limpeza'),
(null, 'administracao'),
(null, 'alimentacao'),
(null, 'seguranca');
insert into EMPR values
(null, 'Rogério', 'Gás', 1, 995827541, '2017-02-14', 'técnico', 8, 'M', '1999-05-12', 32000, 2500, 500),
(null, 'Claudio', 'Roberto', 2, 994827541, '2018-05-11', 'faxineiro', 5, 'M', '1990-12-25', 14400, 200, 100),
(null, 'Rodrigo', 'Henrique', 3, 995827641, '2018-09-29', 'gerente', 12, 'M', '1999-12-04', 50000, 1000, 600),
(null, 'Maria', 'Fernanda', 4, 995487541, '2017-05-20', 'cozinheira', 6, 'F', '1989-12-06', 14400, 2500, 500),
(null, 'Cleiton', 'Xesque', 5, 995647541, '2019-07-12', 'guardinha', 7, 'M', '1986-12-09', 14400, 2500, 500);
In my case, only the administration department has a manager. Thank you from now
Search by subselect with EXISTS https://www.techonthenet.com/mysql/exists.php
– Motta