Skip to main content
Agregar ejemplo con función if
Origen Enlace
jachguate
  • 32.1k
  • 8
  • 41
  • 69

Puedes lograr el resultado concatenando el resultado de dos case, por ejemplo:

SELECT   name
       , IndepYear
       , concat(
           case
             when population is null then ''
             when population > 100000000 then 'large '
             when population > 50000000 then 'medium '
             else 'small '
           end
         , case 
             WHEN IndepYear IS NULL THEN governmentform
             WHEN YEAR(CURDATE()) - IndepYear < 10  THEN CONCAT('New ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear < 50 THEN CONCAT('Modern ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear <= 100 THEN CONCAT('Early ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear > 100 THEN CONCAT('Old ', governmentform)
           END
         ) AS Desc
    from country;

Si la condición es simple, otra opción es concatenar el resultado del if y el case, de esta manera:

SELECT   name
       , IndepYear
       , concat(
           if(population > 100000000, 'large ', '')
         , case 
             WHEN IndepYear IS NULL THEN governmentform
             WHEN YEAR(CURDATE()) - IndepYear < 10  THEN CONCAT('New ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear < 50 THEN CONCAT('Modern ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear <= 100 THEN CONCAT('Early ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear > 100 THEN CONCAT('Old ', governmentform)
           END
         ) AS Desc
    from country;

Puedes lograr el resultado concatenando el resultado de dos case, por ejemplo:

SELECT   name
       , IndepYear
       , concat(
           case
             when population is null then ''
             when population > 100000000 then 'large '
             when population > 50000000 then 'medium '
             else 'small '
           end
         , case 
             WHEN IndepYear IS NULL THEN governmentform
             WHEN YEAR(CURDATE()) - IndepYear < 10  THEN CONCAT('New ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear < 50 THEN CONCAT('Modern ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear <= 100 THEN CONCAT('Early ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear > 100 THEN CONCAT('Old ', governmentform)
           END
         ) AS Desc
    from country;

Puedes lograr el resultado concatenando el resultado de dos case, por ejemplo:

SELECT   name
       , IndepYear
       , concat(
           case
             when population is null then ''
             when population > 100000000 then 'large '
             when population > 50000000 then 'medium '
             else 'small '
           end
         , case 
             WHEN IndepYear IS NULL THEN governmentform
             WHEN YEAR(CURDATE()) - IndepYear < 10  THEN CONCAT('New ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear < 50 THEN CONCAT('Modern ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear <= 100 THEN CONCAT('Early ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear > 100 THEN CONCAT('Old ', governmentform)
           END
         ) AS Desc
    from country;

Si la condición es simple, otra opción es concatenar el resultado del if y el case, de esta manera:

SELECT   name
       , IndepYear
       , concat(
           if(population > 100000000, 'large ', '')
         , case 
             WHEN IndepYear IS NULL THEN governmentform
             WHEN YEAR(CURDATE()) - IndepYear < 10  THEN CONCAT('New ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear < 50 THEN CONCAT('Modern ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear <= 100 THEN CONCAT('Early ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear > 100 THEN CONCAT('Old ', governmentform)
           END
         ) AS Desc
    from country;
Origen Enlace
jachguate
  • 32.1k
  • 8
  • 41
  • 69

Puedes lograr el resultado concatenando el resultado de dos case, por ejemplo:

SELECT   name
       , IndepYear
       , concat(
           case
             when population is null then ''
             when population > 100000000 then 'large '
             when population > 50000000 then 'medium '
             else 'small '
           end
         , case 
             WHEN IndepYear IS NULL THEN governmentform
             WHEN YEAR(CURDATE()) - IndepYear < 10  THEN CONCAT('New ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear < 50 THEN CONCAT('Modern ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear <= 100 THEN CONCAT('Early ', governmentform)
             WHEN YEAR(CURDATE()) - IndepYear > 100 THEN CONCAT('Old ', governmentform)
           END
         ) AS Desc
    from country;