Tomas Hughes February 2016

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."

Answers


Stidgeon February 2016

I think you're close, but there is something wonky about your JOINs - you have a join on 'Car', but that's not one of your tables. JOINing occurs between tables, with ON specifying the fields that are equivalent (what you are JOINing 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.


Andre February 2016

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.

Post Status

Asked in February 2016
Viewed 2,995 times
Voted 4
Answered 2 times

Search




Leave an answer