How to select in the database involving Foreign key and Primary key?

Asked

Viewed 7,311 times

3

I have a problem, I have two tables that are related from the foreign key and the primary key like this:

CREATE TABLE IF NOT EXISTS cargo(
id INT AUTO_INCREMENT NOT NULL,
nome_cargo varchar(50) not null,          "supervisor,gerente, etc..."
salario_cargo DECIMAL NOT NULL,
tipo_cargo VARCHAR(11) NOT NULL,        "administrador ou funcionario comum"
PRIMARY KEY (id)
   );

 CREATE TABLE IF NOT EXISTS funcionario(
id INT AUTO_INCREMENT NOT NULL,
rg VARCHAR(10) NOT NULL,
cpf VARCHAR(11) NOT NULL,
email VARCHAR(255) NOT NULL,
senha VARCHAR(50) NOT NULL,
nome VARCHAR(50) NOT NULL,
id_cargo INTEGER NOT NULL,
PRIMARY KEY (id)
 );
    ALTER TABLE funcionario ADD foreign key (id_cargo) REFERENCES cargo (id);

Every position has an id and the id_position of the employee table serves to identify what the position of this employee, however, I do not know how to select from the employee’s email I can know if he is administrator or common employee, I tried some codes but none worked, someone helps me

1 answer

5

Rotate that query:

SELECT NOME, EMAIL, NOME_CARGO, TIPO_CARGO
FROM FUNCIONARIO 
INNER JOIN CARGO ON CARGO.ID = FUNCIONARIO.ID_CARGO
WHERE EMAIL = '[email protected]'
  • 1

    Man, thank you very much msm, the way I needed to... vlw

Browser other questions tagged

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