SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sort query results according to lowest or highest price

    Hi everyone,

    I'm fetching some data from a MySQL database. One of my columns “prices_from” includes the prices of the products I'm displaying. The type is decimal(6,2). How can I sort the query results according to lowest or highest price? I have heard of the MIN() and MAX() functions but how do I use these in the WHERE part of the query?
    WHERE car=”BMW” && prices_from=......?


    Thank you for your help.

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,824
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by RedBishop View Post
    Hi everyone,

    I'm fetching some data from a MySQL database. One of my columns “prices_from” includes the prices of the products I'm displaying. The type is decimal(6,2). How can I sort the query results according to lowest or highest price? I have heard of the MIN() and MAX() functions but how do I use these in the WHERE part of the query?
    WHERE car=”BMW” && prices_from=......?


    Thank you for your help.
    Try appending "ORDER BY prices_from;" to your SQL statement.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there John,


    thanks for the info. Will try it out.

    Cheers

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Location
    UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By default the list will be in ascending order, so lowest first. To reverse this to highest first or descending, add "DESC" to the end of the query after the column name.
    PHP Code:
    SELECT FROM tablename ORDER BY prices_from DESC 

  5. #5
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks SamA74.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •