consultation with use of Case in Mysql?

0
    select 
      case when precio<2000 then
        (select concat_ws(" ","Tarjetas madres menores a 2000 pesos",  
        (select * from componente inner join tarjetaMadre on
        componente.id_componente=tarjetaMadre.id_componente)) else 
      case when precio<5000 then 
        (select concat_ws(" ","Tarjetas madres menores a 5000 pesos",  
        (select * from componente inner join tarjetaMadre on
        componente.id_componente=tarjetaMadre.id_componente))) else 
      case when precio>5000
        (select concat_ws(" ","Tarjetas madres mayores a 5000 pesos",  
        (select * from componente inner join tarjetaMadre on
        componente.id_componente=tarjetaMadre.id_componente))) end 
    from componente, tarjetaMadre where
    componente.id_componente=tarjetaMadre.id_componente;

mysql I have that case but it marks me an error and I can not decipher it

  

ERROR 1064 (42000): You have an error in your SQL syntax; check the   manual that corresponds to your MySQL server version for the right   syntax to use near:

'else case when precio<5000 then (select concat_ws(" ","Tarjetas madres menores' at line 3 mysql>
    
asked by Diana Lizeth 11.04.2018 в 12:46
source

2 answers

2

You can not use a tuple as an argument of concat_ws . It would only work if in each subquery you choose only one field. On the other hand, it seems that you try to do a grouping by price range, but it is very inefficient that you make the same subquery in each condition.

Finally, the CASE syntax is

CASE
WHEN condicion1 THEN x
WHEN condicion2 THEN y
[ELSE z]
END

If you want to make nested CASE, everyone must have their END

case
when precio<5000 then 'precio menor a 5000'
else case
     when precio<10000 then 'precio menor a 10000'
     else 
         case 
         when precio> 1000 then 'precio mayor a 10000' 
         end
     end
end as rango_precio

(but it is still inefficient to use CASE nested in this case)

Depending on what you want to do, you should probably make a subquery that contains:

select *,
case
when precio<5000 then 'precio menor a 5000'
when precio<10000 then 'precio menor a 10000'
else 'precio mayor a 10000'
end as rango_precio
from 
componente join tarjetamadre using (id_componente)

Sample Fiddle

And from its result, make the corresponding grouping.

    
answered by 11.04.2018 в 13:23
0

You are missing a parenthesis.

Fixed:

    select 
  case when precio<2000 then
    (select concat_ws(" ","Tarjetas madres menores a 2000 pesos",  
    (select * from componente inner join tarjetaMadre on
    componente.id_componente=tarjetaMadre.id_componente))) else 
  case when precio<5000 then 
    (select concat_ws(" ","Tarjetas madres menores a 5000 pesos",  
    (select * from componente inner join tarjetaMadre on
    componente.id_componente=tarjetaMadre.id_componente))) else 
  case when precio>5000
    (select concat_ws(" ","Tarjetas madres mayores a 5000 pesos",  
    (select * from componente inner join tarjetaMadre on
    componente.id_componente=tarjetaMadre.id_componente))) end 
from componente, tarjetaMadre where
componente.id_componente=tarjetaMadre.id_componente;
    
answered by 11.04.2018 в 13:00