Sort at the lowest price in a Mysql table

Asked

Viewed 2,432 times

2

How to list products that have the lowest price for the highest!

From smallest to largest! Using PHP and Mysql.

  • ORDER BY ASC in mysql.

  • You already have something ready and just really need the syntax of your query (which is precisely the answer of Earendul - missing only inform the name of the field) or you want from the connection to the database to the listing of the data in ascending order?

1 answer

2

Just you add this code at the end of your select:

ORDER BY `preco` Asc

In this case, you use ASC to sort in an ascending and DESC to sort in a descending fashion.

A select complete would look like this:

SELECT * FROM `produtos` ORDER BY `preco` Asc

You can see an example using php at this link.

Follow an example in Sqlfiddle: Example here

  • I’ve used (ORDER BY DESC value) for some products I seek it works, but for others not, for example: I searched a headset, and returned 2 headphones, the first on the list has the value of R $ 119,00 but the second is $ 99,00 That is, it is not ordering from the smallest to the largest! In the input field for the user to put the product value, I am using validation (maskMoney). And in the table field I defined as (VARCHAR 255)

  • If possible, post the structure of your table, so you can help better. Use the fiddle with an example, which thus put the solution based on your real problem.

Browser other questions tagged

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