我有这个工作查询
SELECT t1.id as stockid, t1.description as stockdescription, t2.inkoop as price , COUNT(t2.inkoop) as cnt,
(t2.inkoop * COUNT(t2.inkoop)) as totalamount
FROM database1.table_products t1
LEFT JOIN database1.table_stock t2 ON t1.id = t2.stock_id
WHERE 1
GROUP BY t2.inkoop
ORDER BY t1.id ASC
1 个数据库,2 个表:t1 是带有 id 的产品“描述”数据库,描述 t2 是库存,其中有很多以什么价格(购买)的产品并由 stock_id 引用
Output:
id stockdescription price cnt totalamount
1 Product1 1067 15 16005
1 Product1 1290 103 132870
2 Product2 2750 70 192500
3 Product3 500 0 0
但是现在我有了第二个数据库(database2)和第二个库存表(stock2)(与database1.table_stock的结构完全相同)
如何更改我的查询,以便我还可以添加“cnt2”并将总计更改为我的结果?
Like this:
id stockdescription price cnt cnt2 totalcnt totalamount
1 Product1 1067 15 0 15 16005
1 Product1 1290 103 0 103 132870
2 Product2 2750 70 5 75 206250
3 Product3 500 0 4 4 2000