Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2013

    Unanswered: Two select statments in one nested?


    Hopefully someone will be able to help i've been racking my brains out all day!

    Basically I have a table called deals and one called users it's connected via uid (user ID).

    I want to display a set of results to show the deals done by users between two dates I have this working like this:

    SELECT u.username, d.dealdate, SUM(d.dealamount) as total, COUNT(d.dealamount) as dealamount
    FROM users u
    LEFT JOIN deals d ON u.uid = d.userid
    AND d.dealdate BETWEEN '11/08/2013' AND '20/08/2013'
    WHERE isadmin=0
    GROUP BY u.username, d.dealdate
    I want to add a column to the end of this to also show the total amount each user has done for the current month like this:

    SELECT u.username as monthuser, SUM(d.dealamount) as monthtot
    FROM users u
    LEFT JOIN deals d ON u.uid=d.userid 	
    WHERE MONTH(d.dealdate) = MONTH(CURDATE())
    GROUP BY u.username
    ORDER BY monthtot DESC
    These select statements both work but is there a way of joining them so the results display in one statement?

  2. #2
    Join Date
    Jan 2007
    Provided Answers: 10
    General syntax
    SELECT list
         , of
         , columns
    FROM   ( <your_first_sql_statement) As x
      JOIN ( <your_second_sql_statement) As y
        ON y.common_field = x.common_field
    Home | Blog

Posting Permissions

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