Conditional in MYsql clause

-3

I have select normal

SELECT campos FROM tabela_Z WHERE condicoes

I would like to query add a field that is another query .

SELECT 
   campo1,
   campo2,
   campo3,
   (SE houver o id X na TABELA _, = true, caso contrario = false) 
FROM tabela_Z
WHERE condicoes

How to do this?

It would be something like this that I need:

SELECT  
   membros.idMembro,
   membros.nome,
   membros.apelido,
   membros.bloqueado,
   membros.usuario,
   membros.senha,
   acessos.idAcesso,
   acessos.nome
   (SELECT IF(admins.idMembro=1, 's', 'n') FROM admins)
FROM membros , acessos, acessosmembros
WHERE 
   membros.usuario ='caca' AND 
   membros.senha ='aaaa' AND
   acessosmembros.idMembro = membros.idMembro AND
   acessosmembros.idAcesso = acessos.idAcesso

If I do

SELECT IF(admins.idMembro=1, 's', 'n') FROM admins

In phpmyadmin , I have the correct return.

But I need to insert into one of query     

asked by anonymous 04.01.2019 / 20:08

1 answer

0

As you said in comment, a comma was missing after the penultimate select field. To make the query more dynamic, I changed the validation condition, checking if the id of the subquery is equal to the main ( admins.idMembro=acessosmembros ); so it will have the correct value for each user returned (without having to modify the query for each search).

SELECT  
   membros.idMembro,
   membros.nome,
   membros.apelido,
   membros.bloqueado,
   membros.usuario,
   membros.senha,
   acessos.idAcesso,
   acessos.nome,
   (SELECT IF(admins.idMembro=acessosmembros,idMembro, 's', 'n') FROM admins)
FROM membros, acessos, acessosmembros
WHERE 
   membros.usuario ='caca' AND 
   membros.senha ='aaaa' AND
   acessosmembros.idMembro = membros.idMembro AND
   acessosmembros.idAcesso = acessos.idAcesso
    
07.01.2019 / 11:53