COUNT with conditions and selection without WHERE at the end

1

In my table I have 3 columns:

  • % auto_default%
  • id(INT) : values are tipo(INT) or 1
  • 2

I need a medium without the id_usuario(INT) at the end to know if there is a specific data of the user, and if there is a data of it, it displays WHERE if it is tipo(INT) or 1 . >

I can not use 2 at the end because I'm doing a count of the data together. What I currently have:

SELECT 
COUNT(IF(tipo = 1, 1, NULL)) AS X, 
COUNT(IF(tipo = 2, 1, NULL)) AS W, 
COUNT() AS y, -->Conte se existe um dado do usuário especifico na tabela.
tipo AS Z     -->Mostrar dado do usuário especifico se é 1 ou 2.
FROM tabela WHERE id='$id'
    
asked by anonymous 29.09.2018 / 18:03

1 answer

1
SELECT 
COUNT(IF(tipo = 1, 1, NULL)) AS X, 
COUNT(IF(tipo = 2, 1, NULL)) AS W, 
SUM(CASE WHEN id_user = '$id_user' AND tipo = '1' THEN 1 WHEN id_user = '$id_user' AND tipo = '2' THEN 2 ELSE 0 END) AS Y,
FROM curtidas WHERE id_post='$idPost'

After a few tests I came up with this solution, and it is best to put two things together in one, if you have not given 0, if different it shows whether the type is 1 or 2.

    
29.09.2018 / 18:50