Select does not work Mysql

Asked

Viewed 110 times

-3

I’m trying to make the following select

select nm_login,ds_Senha from tb_funcionario where cd_funcionario=?

Only if the field ds_status is the same as admin I want you to ignore cd_funcionario=?, to show all users.

  • Acho que não expliquei direito, teoricamente o que eu quero é isso quando o ds_status for 'admin' quero que ele execute isso
select nm_login,ds_Senha from tb_funcionario;
senao
select nm_login,ds_Senha from tb_funcionario where cd_funcionario=?

  • And this status comes from where? From another table? Or is it a constant?

  • 1

    You have to make use of CASE but as you have not posted the table structure, I have no way to help you please rephrase your question post the structure and explain better the idea.

1 answer

1


You can create a PROCEDURE to handle this situation. Follow a simple example:

inserir a descrição da imagem aqui

Note that this is not the solution to the project because I’m using fictitious field names, it’s just a process that fits the question. Copy and paste only, it will not work unless you are using the same field name (Id_funcionario, nm_login, ds_password, ds_login, Username) and the same table name(tbl_Functions).

Another thing in question is the field "ds_password" for whom I declared the parameter @ds_password of type VARCHAR. If the field/column uses a type of encryption for password vc have to add the function to decrypt before the IF ELSE condition.

Follow below with requested by the colleague...

DELIMITER//
CREATE PROCEDURE Autenticar(@nm_login VARCHAR, @ds_senha VARCHAR)
BEGIN
   DECLARE @ID INT;

   /*Com esse SELECT você captura o ID do funcionário.*/
   SELECT @ID = ID_Funcionario FROM tbl_Funcionarios WHERE Nome_Usuario = @nm_login;

   IF (SELECT ds_status FROM Funcionarios WHERE Nome_Usuario = @nm_login) = 'Admin' THEN
      /*Se o SELECT acima retorna Admin do campo ds_status*/
      SELECT nm_login, ds_senha FROM tbl_Funcionarios;

   ELSE
      /*Se o SELECT acima retorna algo diferente de Admin do campo ds_status*/
      SELECT nm_login, ds_senha FROM tbl_Funcionarios WHERE ID_Funcionario = @ID;
   END IF
END
DELIMITER//
  • Nice, but post the code here in text. Image is bad to read on mobile and almost impossible to copy and paste :)

  • I will edit and add below the image @rodorgas

Browser other questions tagged

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