简体   繁体   中英

Syntax error (missing operator) in query with parenthesized FROM clause

I am working with asp.NET and C# with an access database to create a web application.

My current issue is with an SQL statement. In the project i have already successfully used a string value to build and store a query. Because it is stored in a string it is a bit ugly to look at. The problem is with this last query i am writing. It has 2 inner joins in it and i am unsure what exactly is wrong. The error i get is a syntax error (missing operator) and it then lists everything inside the parenthesis. Here is the query:

SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email],
       Departments.[Department]
FROM (
    Employee INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
    INNER JOIN Departments ON Departments.[Department Number] = '2'
)
WHERE Departments.[Campus]='Clarion';

It is very ugly like this, i know.. Im hoping that since this is a syntax error it wont be too hard.

This query is designed to return the name, email, and department of an employee. The 2 is given with c# code and is determined earlier in the code, but it stands for a certain department. The empDept table goes between the Departments table and the employee table so an employee can be in more than one department.

Any help is greatly appreciated. Thanks

Just remove the parenthesis. They force the database to try to treat the entire expression as a single table, which isn't right.

Also, based on your description, I'd write the query to match the department in the join with what's in the EmpDept table, and then use the WHERE clause to filter down to dept '2'. Right now you filter the Department table down to department '2', but leave it unconditionally related to the rest of the query. This means you're pulling in Employee records from any department.

Finally, I consider it a good practice to get in the habit of using table aliases. Not only does it make your code shorter, but more advanced queries will often pull from more than one instance of the same table, and the aliases make it explicit which instance of the table you mean.

SELECT e.[First Name], e.[Last Name], e.[Email], d.[Department]
FROM Employee e
INNER JOIN EmpDept ed ON e.[EmpUserName] = ed.[EmpUserName]
INNER JOIN Departments d ON d.[Department Number] = ed.[Deptartment Number]
WHERE d.[Campus]='Clarion' AND d.[Department Number] = '2';

Try this

SELECT Employee.[First Name], Employee.[Last Name],
 Employee.[Email], Departments.[Department] 
FROM Employee 
 INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
 INNER JOIN Departments ON EmpDept.[DepartmentId] = Departments.[Id]
WHERE Departments.[Campus]='Clarion' 
AND Departments.[Department Number] = '2'

You'll need an ID on the EmpDept table that matches the Departments table.

SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email], Departments.[Department] 
FROM Employee 
INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName] 
INNER JOIN Departments ON Departments.[Department Number] =  Employee.[Department Number]
WHERE Departments.[Campus]='Clarion' and Departments.[Department Number]=2

There is a small syntax error in squery, it better to join tables with column name and mention column value condition in where clause..

Access requires parentheses within the FROM clause if it includes more than one join. As a first step, try a query like this in the Access query designer.

SELECT
    Employee.[First Name],
    Employee.[Last Name],
    Employee.[Email],
    Departments.[Department]
FROM 
    (Employee
    INNER JOIN EmpDept
    ON Employee.[EmpUserName] = EmpDept.[EmpUserName])
    INNER JOIN Departments
    ON Departments.[Department Number] = '2'
WHERE Departments.[Campus]='Clarion';

I think I placed the parentheses correctly; you can confirm in the query designer. However, I'm puzzled by the second ON clause.

ON Departments.[Department Number] = '2'

That clause doesn't reference any field from the "left" side of the join. I don't understand what it's supposed to accomplish, and I'm unsure whether the db engine will do what you want there.

Try this:

SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email],
    Departments.[Department]
FROM Employee
    INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
    INNER JOIN Departments ON EmpDept.[Department Number] = Departments.[Department Number]
        /* Or whatever your foreign key between Departments and EmpDept is */
WHERE Departments.[Department Number] = '2'
    AND Departments.[Campus] = 'Clarion'

If you have your heart set on a subquery, you need to alias it and make sure it forms a complete query on its own:

SELECT e.[First Name], e.[Last Name], e.Email, e.Department
FROM
(
    SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email],
        Departments.[Department], Departments.Campus
    FROM Employee
        INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
        INNER JOIN Departments ON EmpDept.[Department Number] = Departments.[Department Number]
            AND Departments.[Department Number] = '2'
) AS e
WHERE e.Campus = 'Clarion'

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