简体   繁体   中英

SQL JOIN problem in MS Access 2007

SELECT DISTINCT bw.Bor_name
FROM Borrower AS bw, Loan AS l
JOIN Book_Copy AS bc
ON l.Bc_id = bc.Bc_id
WHERE bw.Bor_id = l.Bor_id
GROUP BY l.Bor_id, bc.Bt_id
HAVING COUNT( bc.Bt_id ) > 1
AND COUNT( l.Bor_id ) > 1;

This works perfectly in a MySQL testing environment but won't work in MS Access 2007 where I actually need it run. I have a few other queries which involve using JOIN and it gives the same error for them as well: "Syntax error in FROM clause".

Edit:

SELECT DISTINCT l.Bor_id
FROM Loan AS l
INNER JOIN Book_Copy AS bc
ON l.Bc_id = bc.Bc_id
GROUP BY l.Bor_id, bc.Bt_id
HAVING COUNT( bc.Bt_id ) > 1
AND COUNT( l.Bor_id ) > 1

Actually this one works fine and gives me the required id number, but what I want to output is the name which is contained in the Borrower table.

I didn't test with Access 2007. However Access 2003 complains about "syntax error in from clause" if I try JOIN alone (without explicitly using INNER JOIN). Seems the database engine requires you designate the JOIN as either RIGHT JOIN, LEFT JOIN, or INNER JOIN; I can't find any other variation it will accept.

Update: I will suggest you create a new query with Access' query designer. Just add the tables and set up the joins. (Leave the GROUP BY and everything else for later.) Then you can switch to SQL View to see the type of join syntax which makes Access happy.

I think you cannot mix implicit and explicit join. you should try with this

SELECT DISTINCT bw.Bor_name
FROM Borrower AS bw
join Loan AS l
on bw.Bor_id = l.Bor_id
JOIN Book_Copy AS bc
ON l.Bc_id = bc.Bc_id
GROUP BY l.Bor_id, bc.Bt_id
HAVING COUNT( bc.Bt_id ) > 1
AND COUNT( l.Bor_id ) > 1;
SELECT DISTINCT bw.Bor_name
FROM Borrower AS bw, Loan AS l
JOIN Book_Copy AS bc
ON (l.Bc_id = bc.Bc_id)
WHERE bw.Bor_id = l.Bor_id
GROUP BY bw.Bor_name, bc.Bt_id
HAVING COUNT( bc.Bt_id ) > 1
AND COUNT( l.Bor_id ) > 1;

Eventually this one did the job. Thanks once again to all who contributed with advice!

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