简体   繁体   中英

SQL: Selecting multiple columns from multiple tables

I am using MS Access 2013.

I am trying to selecting the number and name from Salesperson table. Number, name and postcode from Customers table as well as all the information from the CarSale table all within the past month and order by salesperson no.

I have come up with the following

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;

However as you can see, this is butt-ugly! I did some research and found that I should be using "JOINS" so I went ahead and included them, this is where my problem starts.

After inserting the JOINS into the query I get something that looks like this:

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;

Here are the tables:

**CarSale**
carNo    salespersonNo    customerNo    dateOfSale
-------------------------------------------------------


**Salesperson**
salespersonNo    name    contactNo    monthlySalary  centreNo
--------------------------------------------------------------


**Customer** 
customerNo    name    contactNo   postCode
---------------------------------------------

The error I am getting is "Syntax error in FROM clause."

I think you're close, but there is something wonky about your JOIN s - you have a join on 'Car', but that's not one of your tables. JOIN ing occurs between tables, with ON specifying the fields that are equivalent (what you are JOIN ing ON ). With that in mind:

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;

Notice that your WHERE and ORDER BY are unchanged, and I just used different aliases in my test run. The main difference is in the JOIN - I join from salesperson to CarSales ON the salespersonNo, and then from CarSales to customerNo, similar to what you already have.

The syntax error is because with multiple JOINs you need parentheses around every pair of them.

It would be a lot easier to use the query designer, it does those things automatically.

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;

As Stidgeon wrote, if these are all fields you need, you can omit the join with Car .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM