The topic is as follows, there are two data table sub-table is, user table: Members (Qqnumber,userid); Recharge table: Payment (Payid,userid,paytime,amount); Query for the 2012 months of the most up-to-date users of the Qqnumber, required to summarize according to Qqnumber (hint: A qqnumber may have a number of top-up records, paytime to recharge the time, amount to recharge the amount), please write out the SQL statement. Create two tables First: Then create the stored procedure and insert the test data into the data table:method One:SELECT MAX (amount), T.qqnumber, T.month From (SELECT month (a.paytime) month, B.qqnumber, SUM (a.amount) Amount From payment A, Members B
WHERE A.userid = B.userid and year (a.paytime) = ' A ' GROUP by B.qqnumber, MONTH ' as-T group by T.month; method Two: SELECT M AX (amount), T.qqnumber, T.month From (SELECT month (p.paytime) as MONTH, SUM (p.amount) as amount, m.qqnumber as Qqnumber
From members M JOIN Payment p on m.userid = P.userid
WHERE year (p.paytime) =2012 GROUP by M.qqnumber, MONTH) as T GROUP by T.month; The result of the last query is as follows: This article is the author original, reprint please indicate the source, the wrong place welcome correct, please leave a message!
A more practical MySQL interview: Game data Recharge query