简体   繁体   中英

MS-ACCESS 3 table join

I'm having trouble getting data from 3 tables using a join statement. I would appreciate any help.

Thanks.

I have 3 tables:

Table "Users"
=============
UserID      FirstName   LastName
-----------------------------------------
1       Mick        Mickley
2       Tomy        Hanks
3       Roger       Waters


Table "Appeals"
===============
AppealID    Title       CategoryID  SubmittedByUserID
-----------------------------------------------------------------
1       Title1          2           1
2       Title2          2           1
3       Title3          3           2


And Table "AppealsCategories"
=============================
CatID       CatName     CatDescription
-----------------------------------------
1       CategoryA   CatDescription1
2       CategoryB   CatDescription2
3       CategoryC   CatDescription3

I want to be able to get all the appeals from table "Appeals", that have CategoryID that is related to Category name "CategoryA" (from table "AppealsCategories"). For each appeal, to get all the fields in table "Appeals" and the Firstname and Lastname (from table "Users") that are related to SubmittedByUserID.

I wrote this SQL statement, but it's not working:

SELECT Appeals.*, Users.Firstname, Users.LastName
FROM Users
JOIN Appeals ON Appeals.SubmittedByUserID=Users.UserID
JOIN AppealsCategories ON Appeals.CategoryID=AppealsCategories.CatID
WHERE AppealCategories.CatName='CategoryA';

Any help what I'm doing wrong..?? I am using MS ACCESS

Please help!!! Thanks!!

Like Paul also pointed, you have a typo in your query. Also, brackets, lovely brackets that you need to wrap your joins into:

SELECT Appeals.*, Users.Firstname, Users.LastName
FROM ((Users
        JOIN Appeals ON Appeals.SubmittedByUserID=Users.UserID)
        JOIN AppealsCategories ON Appeals.CategoryID=AppealsCategories.CatID)
WHERE AppealsCategories.CatName='CategoryA';

MS Access has very specific syntax for joins, including specific keywords and parentheses. Try this:

SELECT Appeals.*, Users.Firstname, Users.LastName
FROM (Users INNER JOIN
      Appeals
      ON Appeals.SubmittedByUserID = Users.UserID
     ) INNER JOIN
     AppealsCategories
     ON Appeals.CategoryID = AppealsCategories.CatID
WHERE AppealsCategories.CatName = "CategoryA";

Also the string delimiter is double quotes rather than single quotes.

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