Select does not work Mysql

-3

I'm trying to do the following select

select nm_login,ds_Senha from tb_funcionario where cd_funcionario=?

But if the ds_status field is equal to admin I want it to ignore cd_funcionario=? , to show all users.

    
asked by anonymous 15.01.2018 / 20:59

1 answer

1

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

NotethatthisisnotthesolutiontotheprojectbecauseI'musingdummyfieldnames,it'sjustaprocedurethatfitsthequestion.Copyandpasteonly,itwillnotworkunlessyouareusingthesamefieldname(EmployeeID,nm_login,ds_password,ds_acesso,User_Name)andthesametablename(tbl_Funcionarios).

Anotherthinginquestionisthefield"ds_senha" for whom I declared the @ds_password parameter of type VARCHAR. If the field / column uses an encryption type for password you have to add the function to decrypt before the IF ELSE condition.

Follow below as requested by your 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//
    
16.01.2018 / 02:08