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`
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.
– Williams
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.
– Tércio Costa
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
– Ivan Ferrer
It would be something like: CASE WHEN '78000-000' BETWEEN
Transportadora
.cep_inicio
ANDTransportadora
.cep_fim
THEN '78000-000' ELSE NULL END AS cep_sort– Ivan Ferrer