簡體   English   中英

如何從另一個MySQL減去一行

[英]How to subtract the one row from another MySQL

鑒於附表

在此處輸入圖片說明

如何找到每天出售的Mac和Windows之間的區別。 有人可以解釋一下邏輯嗎

內部聯接將完成這項工作。

SELECT 
WindowsTable.Date,
ABS(WindowsTable.Sold - MacTable.Sold) absoluteDifference 
FROM
(SELECT 
*
FROM producttable
WHERE Products = 'Windows') WindowsTable

INNER JOIN 

(
SELECT 
*
FROM producttable
WHERE Products = 'Mac' ) MacTable

ON WindowsTable.Date = MacTable.Date;

此處演示

嘗試在日期使用INNER JOIN重塑查詢:

SELECT macs_sales.Date, (MacsSold - WindowsSold) AS sales_difference
FROM
(
  SELECT Date, Sold as MacsSold
  FROM computer_sales
  WHERE Products="Mac"
) macs_sales
INNER JOIN
(
  SELECT Date, Sold as WindowsSold
  FROM computer_sales
  WHERE Products="Windows"
) windows_sales
ON macs_sales.Date = windows_sales.Date

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM