Unrecognized keyword "as"

Asked

Viewed 88 times

-1

I am using MYSQL together with Phpmyadmin 4.7.4, and it accuses that the keyword 'as' is not recognizable, but when running it is in infinite loop, and shows no result.

   SELECT 
    c.`nome`, 
    case c.`id_estado`
        when c.`id_estado` = 1 then 12 
        when c.`id_estado` = 2 then 27
        when c.`id_estado` = 3 then 13 
        when c.`id_estado` = 4 then 16 
        when c.`id_estado` = 5 then 29
        when c.`id_estado` = 6 then 23
        when c.`id_estado` = 7 then 53 
        when c.`id_estado` = 8 then 32 
        when c.`id_estado` = 9 then 52 
        when c.`id_estado` = 10 then 21
        when c.`id_estado` = 11 then 31
        when c.`id_estado` = 12 then 50 
        when c.`id_estado` = 13 then 51
        when c.`id_estado` = 14 then 15 
        when c.`id_estado` = 15 then 25 
        when c.`id_estado` = 16 then 26 
        when c.`id_estado` = 17 then 22 
        when c.`id_estado` = 18 then 41 
        when c.`id_estado` = 19 then 33 
        when c.`id_estado` = 20 then 24 
        when c.`id_estado` = 21 then 11 
        when c.`id_estado` = 22 then 14 
        when c.`id_estado` = 23 then 43 
        when c.`id_estado` = 24 then 42 
        when c.`id_estado` = 25 then 28 
        when c.`id_estado` = 26 then 35 
        when c.`id_estado` = 27 then 17
    end as teste,
    c.`cod_municipio`, cll.`latitude`, cll.`longitude`
    FROM `cidade` c
    INNER JOIN  cidades_lat_long cll ON c.`cod_municipio` = cll.id_geocode

inserir a descrição da imagem aqui

I’m using this code example https://talibamartins.wordpress.com/2007/09/20/if-e-case-no-mysql/

The structure

CREATE TABLE cidades_lat_long (
    id_geocode int(11) NOT NULL,
    latitude double NOT NULL,
    longitude double NOT NULL,
    PRIMARY KEY (id_geocode)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE cidade (
    id int(11) NOT NULL AUTO_INCREMENT,
    nome varchar(45) NOT NULL,
    id_estado int(11) NOT NULL,
    cod_municipio int(11) DEFAULT NULL COMMENT 'CEP',
    PRIMARY KEY (`id`),
    KEY fk_cidade_estado(id_estado)
) ENGINE=InnoDB AUTO_INCREMENT=10036 DEFAULT CHARSET=utf8;

2 answers

2

If you’re using the case based on a field, do not need to repeat the field in each when:

 SELECT 
    c.`nome`, 
    case c.`id_estado`
     WHEN 1 THEN 12
     WHEN 2 THEN 27
     WHEN 3 THEN 13
     WHEN 4 THEN 16
     WHEN 5 THEN 29
     WHEN 6 THEN 23
     WHEN 7 THEN 53
     WHEN 8 THEN 32
     WHEN 9 THEN 52
     WHEN 10 THEN 21
     WHEN 11 THEN 31
     WHEN 12 THEN 50
     WHEN 13 THEN 51
     WHEN 14 THEN 15
     WHEN 15 THEN 25
     WHEN 16 THEN 26
     WHEN 17 THEN 22
     WHEN 18 THEN 41
     WHEN 19 THEN 33
     WHEN 20 THEN 24
     WHEN 21 THEN 11
     WHEN 22 THEN 14
     WHEN 23 THEN 43
     WHEN 24 THEN 42
     WHEN 25 THEN 28
     WHEN 26 THEN 35
     WHEN 27 THEN 17
    end as teste,
    c.`cod_municipio`, cll.`latitude`, cll.`longitude`
    FROM `cidade` c
    INNER JOIN  cidades_lat_long cll ON c.`cod_municipio` = cll.id_geocode 

See a fiddle working: http://sqlfiddle.com/#! 9/b8a28d/8

EDIT: copied the complete link code @Sorack posted

  • Doesn’t seem to answer the question.

  • 1

    Thank you @Sorack, "blatantly" copied :)

  • 1

    @Ericwu, the script works, I believe it solves the problem. There is no error in the question script in the part end as teste, but when I tested in fiddle does not bring the correct results exactly on account of the conditional made within each when, and this is explained in the reply

  • If the answer explains the problem of when, brings a working solution, including with an example in fiddle, who voted negative could at least take the trouble to explain the vote

  • Thank you very much for your attention, it’s a great tip, but the problem 'the tests' persists, I’m sending an Issue to phpmyadmin’s Github because apparently it seems to be a bug of their parser, since in other places the query is correct

1

Would that be:

SELECT 
c.`nome`, 
c.`cod_municipio`, 
cll.`latitude`, 
cll.`longitude`,
c.`id_estado` as teste
CASE teste
    WHEN 1 THEN 12 
    WHEN 2 THEN 27
    WHEN 3 THEN 13 
    WHEN 4 THEN 16 
    WHEN 5 THEN 29
    WHEN 6 THEN 23
    WHEN 7 THEN 53 
    WHEN 8 THEN 32 
    WHEN 9 THEN 52 
    WHEN 10 THEN 21
    WHEN 11 THEN 31
    WHEN 12 THEN 50 
    WHEN 13 THEN 51
    WHEN 14 THEN 15 
    WHEN 15 THEN 25 
    WHEN 16 THEN 26 
    WHEN 17 THEN 22 
    WHEN 18 THEN 41 
    WHEN 19 THEN 33 
    WHEN 20 THEN 24 
    WHEN 21 THEN 11 
    WHEN 22 THEN 14 
    WHEN 23 THEN 43 
    WHEN 24 THEN 42 
    WHEN 25 THEN 28 
    WHEN 26 THEN 35 
    WHEN 27 THEN 17
END  
FROM `cidade` c
INNER JOIN  cidades_lat_long cll 
ON c.`cod_municipio` = cll.id_geocodee
  • Why would your answer work with regard to the errors pointed out?

  • Mutio thanks for your attention, but doesn’t seem to solve the problem >You have a syntax error in your SQL next to 'CASE test'

  • missing a comma dps from as teste, this according w3scools

Browser other questions tagged

You are not signed in. Login or sign up in order to post.