-1

発注書と発注書のテーブルを入手しました。

販売注文

Date   Amount

 26/10  120
 26/10  130 
 26/10  50
 27/10  50 
 27/10  60

注文書

Date   Amount

 26/10  200 
 26/10  300 
 26/10  100
 27/10  100 
 27/10  100

日付ごとの総売上高と総購入額を表示したい。

例えば

 Date   Total Sales Total Purchases 
  26/10    300            600 
  27/10    110            200

私のSQLクエリはこのようなものですが、機能しません。

$query = "Select o.date, o.Amount AS Sales, p.date, p.Amount AS Purchases
          From customerorder o
          right join Purchaseorder p on o.date = p.date
          order by o.date";

助けてください!ありがとう!

4

2 に答える 2

1
    SELECT COALESCE(o.date, p.date) date, Sales, Purchases
      FROM (SELECT date, SUM(amount) Sales FROM CustomerOrder GROUP BY date) o
 FULL JOIN (SELECT date, SUM(amount) Purchases FROM PurchaseOrder GROUP BY date) p
        ON o.date = p.date
  ORDER BY date

MySQLはFULLJOINをサポートしていないため、特にMySQLの場合は、

    SELECT o.date, Sales, Purchases
      FROM (SELECT date, SUM(amount) Sales FROM CustomerOrder GROUP BY date) o
 LEFT JOIN (SELECT date, SUM(amount) Purchases FROM PurchaseOrder GROUP BY date) p
        ON o.date = p.date
 UNION ALL
    SELECT date, NULL, SUM(amount) Purchases
      FROM PurchaseOrder p2
     WHERE NOT EXISTS (SELECT *
                       FROM CustomerOrder o2
                       WHERE o2.date = p2.date)
  GROUP BY date
  ORDER BY date
于 2012-10-27T02:54:41.080 に答える
0
select c.date,total_sales,total_purchase from 
(select date,sum(amount) total_sales from customerorder group by date) c
join
(select date,sum(amount) total_purchase from purchaseorder group by date) p
on c.date=p.date
order by c.date

スキーマがわからないので、「join」を「rightjoin」に変更するとよいでしょう。

于 2012-10-27T02:54:01.283 に答える