www.webdeveloper.com
Results 1 to 2 of 2

Thread: Mysql if in select query problem

  1. #1
    Join Date
    Jun 2008
    Posts
    207

    Mysql if in select query problem

    I'm trying to build a single select statement, that will all build totals for various dates. Here is what i have:


    SELECT
    *,
    if(date = '2011-08-16', sum(rev),0) as todaysRev,
    if(date = '2011-08-15', sum(rev),0) as yRev,
    if(date = '2011-08-14', sum(rev),0) as 2dayRev

    FROM income
    WHERE date BETWEEN '2011-08-15' AND '2011-08-16'
    GROUP BY location


    The problem is that the it will always attribute the values to the first date it finds and not the other two dates.

    Any help would be much appreciated.

  2. #2
    Join Date
    Jun 2008
    Posts
    136
    I think you could use a better query:

    SELECT
    *, sum(rev) as rev,
    FROM income
    WHERE date BETWEEN '2011-08-15' AND '2011-08-16'
    GROUP BY location, date

    Then when you are fetching the data, if "rev" column was equal to 2011-08-16, count it as todayRev and so on.

    Hope this helps.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles