SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: SYNTAX help

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SYNTAX help

    I'm not sure how to word this query.

    i need it to say something like

    select column1 from tablename where colum2= MIN(column2)

    in other words i need it to select column info from column 1 where it is = to the lowest value in column 2.

    the value of column 2 will change everyday but i already have that part working.

    thank you.


    Also can i tell it to grab 2 rows of info?

  2. #2
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
       select column1 
     from tablename 
     where colum2 = 
     (select MIN(column2) from tablename)

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can i tell it to grab 2 rows of info?

  5. #5
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you mean? If there is more than one row in the table with the same min value for column two then all rows are returned containing that value. If you want to limit which ones are returned how do you choose? If it doesn't matter other than you want to show two rows for col1 that have same min value for col2 then just add a LIMIT 2 at the end of the query.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basically I just want to pull the information for the two rows that have the lowest value. they dont necessarily have to have the same value.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    select column1,column2 from tablename
    order by column2 limit 2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    select column1,column2 from tablename
    order by column2 limit 2

    This query works when i put it in Dreamweaver or MYSQL CC but when i put the page to the browser it gives me this message:

    You have an error in your SQL syntax near 'LIMIT 0, 2' at line 1

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    "put the page to the browser" ????

    any chance you could show the actual query?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    "put the page to the browser" ????

    any chance you could show the actual query?
    I meant put the page on the server and then opened it in the browser.

    sure here it is:

    $query_gettest = "SELECT `test` FROM `test_track` WHERE `cat`= '$cat' AND `speed`= '$speed' AND `date`='$date' ORDER BY `id` LIMIT 2";


    when i run the query in mysql it works fine and even in the query builder window of Dreamweaver MX. However, when i pull the page up in the browser it gives me the error.

  11. #11
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    that query looks okay

    the problem is likely your php code
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •