need to get data from multiple mySQL tables grouping by date

时间:2015-11-12 11:52:23

标签: mysql sql phpmyadmin

I have 2 tables

1st: order columns: id, date, price

2nd: paypal columns: id, posted_date, amount

the columns date and posted_date contains the full date & time details; day/month/year hrs:minute:seconds

I need to get the data by grouping by the day from the both tables

  1. order.date (day by day)
  2. count all orders from order table for each day
  3. sum of all price records from order table for each day
  4. sum of all amount records from the another table paypal table for the same days

I can't imagine if I should use join, union, union all, or just merge by comma

2 个答案:

答案 0 :(得分:0)

 SELECT DATE(O.`dater`) AS Dates,
    COUNT(O.orders) AS Order_count,
    SUM(O.price) as Total_Price,
    (SELECT SUM(amount) FROM paypal WHERE DATE(O.`dater`)=`posted_date`) AS Total_Amount
    FROM orders O
    GROUP BY DATE(O.`dater`)

Note:(I have used column dater instead of column date) Hope this helps.

答案 1 :(得分:0)

这个为我工作:)

SELECT DATE(O.`date`) AS Dates,
COUNT(O.order) AS Order_count,
SUM(O.price) as Total_Price,
(SELECT SUM(amount) FROM paypal WHERE DATE(O.`date`)=`posted_date`) AS Total_Amount
FROM order O
GROUP BY DATE(O.`date`)