Sort result using CASE and BETWEEN

Asked

Viewed 606 times

1

In the first code just below, I can list the freight price of the table transport via the band cep and peso, however I am having a problem, I need to sort the result according to how cep sent, depending on the query this falling in the second line and I need it to be in the first, I am not knowing how to use the function CASE along with BETWEEN for the first position later in the clause ORDER BY

This way it works right, without the CASE

SELECT

`Transportadora`.*,

FROM `envio_transportadora` AS `Transportadora` 
    INNER JOIN `shop_envio` AS `ShopEnvio` 
ON (`ShopEnvio`.`id_envio` = `Transportadora`.`id_envio_default` AND `ShopEnvio`.`ativo` = 'True' AND `ShopEnvio`.`id_shop_default` = '5')  
    WHERE 
'78000-000' BETWEEN `Transportadora`.`cep_inicio` AND `Transportadora`.`cep_fim` AND 13 BETWEEN `Transportadora`.`peso_inicial` AND `Transportadora`.`peso_final`

Already with the CASE of the syntax error in query goes below:

SELECT

`Transportadora`.*,

((CASE WHEN '78000-000' BETWEEN `Transportadora`.`cep_inicio` AND `Transportadora`.`cep_fim`)) AS cep_ordenar 

FROM `envio_transportadora` AS `Transportadora` 
    INNER JOIN `shop_envio` AS `ShopEnvio` 
ON (`ShopEnvio`.`id_envio` = `Transportadora`.`id_envio_default` AND `ShopEnvio`.`ativo` = 'True' AND `ShopEnvio`.`id_shop_default` = '5')  
    WHERE 
'78000-000' BETWEEN `Transportadora`.`cep_inicio` AND `Transportadora`.`cep_fim` AND 13 BETWEEN `Transportadora`.`peso_inicial` AND `Transportadora`.`peso_final`

2 answers

2


You are making the use of CASE in the wrong way, the correct one would be something like this:

CASE condicao
 WHEN valor1 THEN 'valor1'
 WHEN valor2 THEN 'valor2'
 ELSE 'valor_alternativo'
END

And even then, there is no need to use this to sort data, moreover, in mysql there are other ways to make use of conditions such as use of IF, IFNULL, etc. Example:

 IF('condicao', valor_if_dondicao, 'valor_else_condicao') AS saida
To sort you don’t need any of this, just use ORDER BY at the end of your query and put the order preference list, example:

ORDER BY `Transportadora`.`cep_inicio` ASC, `Transportadora`.`cep_fim` ASC

Or make an order by more customized by the field:

ORDER BY FIELD (`Transportadora`.`cep_inicio`,'78000-000','79000-000','...') ASC,
         FIELD (`Transportadora`.`cep_fim`,'...','...') ASC

-1

I believe that you do not know how to use CASE. With case we can buy a certain value and have a result according to this value. For example:

CASE numero
  WHEN 1 THEN 'um'
  WHEN 2 THEN 'dois'
  ELSE 'outro'
END

Thus we can compare a column, which in the case was number, to several values and depending on the value return another specific value. Besides, I didn’t see any ORDER BY clause.

Go to my blog https://oraclepress.wordpress.com which has several SQL things

  • Knowing that I am using CASE incorrectly, this I know, even because, before asking I researched a lot, here is an example with CASE and BETWEEN, I just thought I could adapt to my problem, and for those who understand SQL, order by is the least in the example. Now posting a personal blog that talks about oracle does not resolve.

  • I’m sorry if I offended you, I just want to help. Could you explain how you would like to use CASE with BETWEEN and ORDER BY? I will be grateful to be able to help you. I just didn’t understand very well what you wanted.

  • Good that you know, because for CASE, the correct thing would be to use the between with WHEN 'cep_sort' and not AS 'cep_sort'. : D

  • It would be something like: CASE WHEN '78000-000' BETWEEN Transportadora.cep_inicio AND Transportadora.cep_fim THEN '78000-000' ELSE NULL END AS cep_sort

Browser other questions tagged

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