-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
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;
Doesn’t seem to answer the question.
– Eric Wu
Thank you @Sorack, "blatantly" copied :)
– Ricardo Pontual
@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 eachwhen
, and this is explained in the reply– Ricardo Pontual
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– Ricardo Pontual
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
– Sveen