SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: MIN() problem

Hybrid View

  1. #1
    SitePoint Addict n0other's Avatar
    Join Date
    Feb 2005
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MIN() problem

    Code:
    SELECT 
     f.file, 
     i.last_access, 
     i.id AS pid 
    FROM 
     files f, 
     images i 
    WHERE 
      f.pid = i.id 
     AND 
      f.area = (SELECT MIN(area) FROM files WHERE pid = i.id)
    But this returns all rows, not only the ones with minimum area value, if I add "GROUP BY pid" to the main query, only rows with bigger area values are returned (I currently have 2 different values for area). How could it be fixed?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    your query is fine, and requires no GROUP BY

    are you sure it isn't a data problem?

    do you have a test script of sample rows that illustrate the problem you're seeing?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict n0other's Avatar
    Join Date
    Feb 2005
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank's r937, it was a data problem after all. I was also using varchar to store area values, because of that (I guess), values were compared as strings.


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
  •