我有3个表格可以从中获取数据,
mstr_store(new_site, Site_name...)
Sale_fy1819(New_site, MC_Code, Net_Sale_Units, dt...)
Sale_fy1920(New_site, MC_Code, Net_Sale_Units, dt...)
我期望得到以下结果,
--------------------------------------------------------------------
Store | MC_Code | Net_sale_units (fy1819) | Net_sale_units (fy1920)
--------------------------------------------------------------------
我已经编写了以下代码,但显示错误1066,请帮助我了解编写这些类型的查询的正确方法。
SELECT
mstr_store.NEW_SITE,
sale_fy1819.MC_Code,
sale_fy1819.Net_Sales_Units,
sls_fy1920.Net_Sales_Units
FROM
mstr_store,
sale_fy1819,
sls_fy1920
INNER JOIN
sale_fy1819 ON mstr_store.NEW_SITE = sale_fy1819
INNER JOIN
sls_fy1920 ON sale_fy1819.Store_Code = sls_fy1920.Store_Code
WHERE
sale_fy1819.DT >= '2018-09-01'
AND sale_fy1819.dt <= '2018-09-30'
AND sls_fy1920.dt >= '2019-09-01'
AND sls_fy1920 <= '2019-09-30';
最佳答案
FROM
mstr_store,
sale_fy1819,
sls_fy1920
INNER JOIN
sale_fy1819 ON mstr_store.NEW_SITE = sale_fy1819
INNER JOIN
sls_fy1920 ON sale_fy1819.Store_Code = sls_fy1920.Store_Code
您已经两次编写了sls_fy1920和sale_fy1819。您应该删除它们。
FROM
mstr_store
INNER JOIN
sale_fy1819 ON mstr_store.NEW_SITE = sale_fy1819
INNER JOIN
sls_fy1920 ON sale_fy1819.Store_Code = sls_fy1920.Store_Code
关于mysql - 内联3 table ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58355152/