如何从另一个MySQL中减去一行

时间:2016-04-05 03:20:33

标签: mysql sql

鉴于附表是

enter image description here

如何找到每天销售的Mac和Windows之间的差异。 有人可以解释逻辑

2 个答案:

答案 0 :(得分:2)

INNER JOIN可以胜任。

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;

https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/

答案 1 :(得分:1)

尝试在日期使用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