Search value corresponding to weight

Asked

Viewed 55 times

2

I have a table with values, where each value is according to weight (Kg).

Up to 3kg = R$20.00

Up to 5kg = R$25.00

Up to 10kg = R$28.00

SELECT estado, kg, valorCap, valorExcedCap, valorAloremCap, prazoCap 
  FROM transportadoras_valores 
    WHERE id_transportadora = '1' && estado = 'AL' && kg = '4'

In this example above, I need to return the value of 5kg.

I have an online example:

http://sqlfiddle.com/#! 9/06ce6/5

3 answers

5


You can do it like this:

SELECT estado, kg, valorCap, valorExcedCap, valorAloremCap, prazoCap 
  FROM transportadoras_valores 
  WHERE id_transportadora = 1 && estado = 'AL' && kg >= 4
  ORDER BY kg ASC
  LIMIT 1

That way you say you want it to be a single result, the first to be greater or equal to the value you passed to the query.

  • 1

    That, LIMIT 1 was missing

  • And the sign of >=

  • @Tiago and ORDER BY.

  • 1

    Thank you so much again

-1

SELECT estado, kg, valorCap, valorExcedCap, valorAloremCap, prazoCap 
FROM transportadoras_valores 
WHERE id_transportadora = '1' && estado = 'AL' && kg > '3' && kg <= '5')
  • Luiz, it can’t be like this, because it’s never the exact number. A box can have 4 kilos and 500 grams (4,500 grams).

  • I think he needs a condition to give the first value greater than a certain weight to calculate the price, and not change the select from 4 to 5 manually.

-1

SELECT estado, kg, valorCap, valorExcedCap, valorAloremCap, prazoCap
    FROM transportadoras_valores 
    WHERE id_transportadora = '1' && estado = 'AL' && kg = '5'
  • I think he needs a condition to give the first value greater than a certain weight to calculate the price, and not change the select from 4 to 5 manually.

  • Sebastia, it can’t be like that, because it’s never the exact number. A box can have 4 kilos and 500 grams (4,500 grams).

Browser other questions tagged

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