简体   繁体   中英

Query returning syntax error in FROM clause

I have 3 tables with various columns:

tableA = id(PK) & name columns
tableB = id(PK), A_ID(foreign key to tableA), name, address, etc columns
tableC = id(PK), A_ID(foreign key to tableA), name columns

I'm trying to use the following query to retrieve values from certain columns within all tables based on tableA name = 'something', but always returning syntax errors.

“SELECT tableA.name, tableB.name, tableB.address, tableC.name FROM 
tableA, tableB, tableC JOIN tableB ON tableA.id = tableB.A_ID JOIN tableC 
ON tableA.id = tableC.A_ID WHERE tableA.name = ‘something’”  

You have to remove tables from from statement if you want to use join syntax

SELECT tableA.name, tableB.name, tableB.address, tableC.name 
FROM tableA
INNER JOIN tableB ON tableA.id = tableB.A_ID 
INNER JOIN tableC ON tableA.id = tableC.A_ID 
WHERE tableA.name = 'something'

I suggest you to use aliases, the code could be more readable:

SELECT A.name, B.name, B.address, C.name 
FROM tableA A
INNER JOIN tableB B ON A.id = B.A_ID 
INNER JOIN tableC C ON A.id = C.A_ID 
WHERE A.name = 'something'

Ms Access requires you to specify the type of join: INNER ; LEFT ; or RIGHT . Access does not recognize just JOIN as a synonym for INNER JOIN .

A query which includes more than one join requires parentheses in the FROM clause.

I also changed your quote characters to plain " and ' . The sample query included type-setting quotes. I don't know if they are present in the actual SQL, but I would avoid them.

SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM 
    (tableA
    INNER JOIN tableB
    ON tableA.id = tableB.A_ID)
    INNER JOIN tableC
    ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'

If you have the full version of Access available, use the query designer to set up the joins. The designer knows the syntax rules which keep the db engine happy.

otherwise you could do it like:

SELECT tableA.name, tableB.name, tableB.address, tableC.name 
FROM tableA, tableB, tableC 
WHERE tableA.id = tableB.A_ID AND tableA.id = tableC.A_ID 
AND tableA.name = ‘something

Parado's answer is correct..

Moreover you can simplify the length of the query by using aliases for the table names.. And it is good practice too..

select a.name, b.name, b.address from tableA a join tableB b on a.id = b.A_ID

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