SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,868
    Mentioned
    37 Post(s)
    Tagged
    0 Thread(s)

    MySql Sum problem

    Hey guys

    I have an issue. I have a mysql select like this:

    SELECT sum(points) as p_max, sum(achieved) as p_ach FROM SSL_A6supplier_ratings where customer_id='$customer_id' and supplier_id='$supplier_id' and theme_id=1 order by ID desc limit 8

    its and archive table, so it must only sum the last 8, but for some reason the limit doesnt work and its adding everything up.

    I did some googling and it seems that the limit doesnt work for sum or count etc, and i have to use sub-selects, but i cnt seem to get it to work.

    I have tried this, but doesnt work:

    SELECT sum(select points FROM SSL_A6supplier_ratings where customer_id=1 and supplier_id=1 and theme_id=1 order by ID desc limit 8) as p_max, sum(select achieved FROM SSL_A6supplier_ratings where customer_id=1 and supplier_id=1 and theme_id=1 order by ID desc limit 8) as p_ach FROM SSL_A6supplier_ratings where customer_id=1 and supplier_id=1 and theme_id=1 order by ID desc limit 8

    Plz help me out. I am not great with mysql and sub-selects

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    695
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select sum(points) as p_max, sum(achieved) as p_ach 
      from (select points, 
                   achieved
              from SSL_A6supplier_ratings 
             where customer_id = '$customer_id' 
               and supplier_id='$supplier_id' 
               and theme_id = 1 
             order by ID desc 
             limit 8) dt

  3. #3
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,868
    Mentioned
    37 Post(s)
    Tagged
    0 Thread(s)
    ah. Ok, i see. Thanks a lot for the help. I came close to that since i posted, but still no cigar until now. Thanks


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
  •