我正在使用MS Access 2013。
我正在尝试从Salesperson表中选择数字和名称。来自Customers表的编号,名称和邮政编码以及CarSale表中过去一个月内的所有信息以及销售员编号的订单。
我想出了以下内容
SELECT CS.carNo, CS.dateOfSale, SA.salespersonNo, SA.name AS SalesName,
CU.customerNo, CU.name AS CustName, CU.postCode
FROM CarSale AS CS, Car AS C, Salesperson AS SA, Customer AS CU
WHERE CS.carNo = C.carNo AND CS.salespersonNo = SA.salespersonNo
AND CS.customerNo = CU.customerNo AND dateOfSale BETWEEN #01/09/2016#
AND #02/09/2016#
ORDER BY CS.salespersonNo;
然而,正如你所看到的,这很丑陋!我做了一些研究,发现我应该使用" JOINS"所以我继续把它们包括在内,这就是我的问题所在。
将JOINS插入查询后,我得到的内容如下:
SELECT CS.carNo, CS.dateOfSale, SA.salespersonNo, SA.name AS SalesName,
CU.customerNo, CU.name AS CustName, CU.postCode
FROM CarSale AS CS
JOIN Car AS C ON CS.carNo = C.carNo
JOIN Salesperson AS SA on CS.salespersonNo = SA.salespersonNo
JOIN Customer AS CU ON CS.customerNo = CU.customerNo
WHERE cs.dateOfSale BETWEEN #01/09/2016# AND #02/09/2016#
ORDER BY CS.salespersonNo;
以下是表格:
**CarSale**
carNo salespersonNo customerNo dateOfSale
-------------------------------------------------------
**Salesperson**
salespersonNo name contactNo monthlySalary centreNo
--------------------------------------------------------------
**Customer**
customerNo name contactNo postCode
---------------------------------------------
我得到的错误是" FROM子句中的语法错误。"
答案 0 :(得分:0)
我认为你已经离开了,但是你的if (Workbook.SharedStrings.si[Convert.ToInt32(_value)].t != null)
{
Text = Workbook.SharedStrings.si[Convert.ToInt32(_value)].t;
}
else if (Workbook.SharedStrings.si[Convert.ToInt32(_value)].ns != null)
{
for (int i = 0; i < Workbook.SharedStrings.si[Convert.ToInt32(_value)].ns.Length; i++)
{
Text += Workbook.SharedStrings.si[Convert.ToInt32(_value)].ns[i].t;
}
}
有一些不可思议的东西 - 你加入了汽车&#39;但这不是你的一个表。表之间发生JOIN
,ON指定相等的字段(JOIN
JOIN
ON
}。考虑到这一点:
SELECT s.salespersonNo, s.name, c.customerNo, cs.carNo,
cs.dateofsale, c.name, c.postCode
FROM salesperson s
INNER JOIN carsale cs
ON cs.salespersonNo = s.salespersonNo
INNER JOIN customer c
ON cs.customerNo = c.customerNo
WHERE cs.dateOfSale BETWEEN #01/09/2016# AND #02/09/2016#
ORDER BY CS.salespersonNo;
请注意,您的WHERE
和ORDER BY
未更改,我在测试运行中只使用了不同的别名。主要区别在于JOIN
- 我从销售人员加入CarSales ON
salespersonNo,然后从CarSales加入customerNo,类似于您已有的。
答案 1 :(得分:0)
语法错误是因为对于多个JOIN,您需要在每对JOIN周围使用括号。
使用查询设计器要容易得多,它会自动执行这些操作。
SELECT CS.carNo, CS.dateOfSale, SA.salespersonNo, SA.name AS SalesName,
CU.customerNo, CU.name AS CustName, CU.postCode
FROM (((CarSale AS CS
INNER JOIN Car AS C ON CS.carNo = C.carNo)
INNER JOIN Salesperson AS SA on CS.salespersonNo = SA.salespersonNo)
INNER JOIN Customer AS CU ON CS.customerNo = CU.customerNo)
WHERE cs.dateOfSale BETWEEN #01/09/2016# AND #02/09/2016#
ORDER BY CS.salespersonNo;
正如Stidgeon所写,如果这些都是您需要的字段,则可以省略Car
的加入。