PHP/Mysql Select products below a value in the table

Asked

Viewed 164 times

0

I need to select the products with the value below 300 reais in which column situacao be equal to 2:

Example: in the shopping table I have 8 products registered:

1 sapato valor 25 situacao 1    
2 calça valor 50 situacao 2    
3 meia valor 60 situacao 2    
4 sandália valor 70 situacao 1    
5 bicicleta valor 120 situacao 2    
6 pc core 2 duo valor 250 situacao 2    
7 notebook valor 500 situacao 2    
8 ipad 8 gb valor 600 situacao 2

I tried to do it like this, but it didn’t work:

$query = mysql_query("SELECT valor, MAX($valor) FROM ofertas WHERE situacao = '2'") or print (mysql_error());
$query = mysql_query("SELECT valor,FROM ofertas WHERE  MAX($valor) AND situacao = '2'") or print (mysql_error());

Being that I need it to be done in the format above or as simple as possible.

  • Try this query: SELECT * FROM ofertas WHERE situacao=2 AND valor < 300 ORDER BY valor ASC that is, selects all values below 300 whose situation is equal to "2" and sorts by the column "value".

  • Be careful with your titles, because yours does not reflect the content of the question, because you do not want to select the maximum value, but products below a value

  • Hello La Souza. See how to ask a good question, so it will be easier to help you.

  • In the last example in $query = mysql_query("SELECT value,FROM offers WHERE MAX($value) AND situacao = '2'") or print (mysql_error());, is running comma in value, from?

  • This was the wrong way to do it, which I posted just to show how I was doing it wrong. Thank you.

2 answers

0

So to get the values below 300 real, just do this:

SELECT * FROM tabela WHERE valor < 300 AND outrasCondicoes...;

To take with the highest value:

SELECT max(valor) FROM tabela WHERE outrasCondicoes...

To get the most value, below the 300 real:

SELECT max(valor) FROM tabela WHERE valor < 300 AND outrasCondicoes...

Would that be?

0

I need to select the products with the value below 300 reais in which the situation column is equal to 2:

$query = mysql_query("SELECT * FROM ofertas WHERE situacao LIKE '2' AND valor < 300") or print (mysql_error());

This deduces that your code is correct at this point and the situation is in text format, if not, instead of using LIKE '2' you should only use = 2, being like this:

$query = mysql_query("SELECT * FROM ofertas WHERE situacao = 2 AND valor < 300") or print (mysql_error());

The same goes for the 300, if it is in text format you should use LIKE '300'

Browser other questions tagged

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