3

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

4

5 回答 5

5

只需删除括号。他们强制数据库尝试将整个表达式视为单个表,这是不正确的。

另外,根据您的描述,我将编写查询以将连接中的部门与 EmpDept 表中的内容相匹配,然后使用 WHERE 子句过滤到部门“2”。现在,您将 Department 表过滤到部门“2”,但使其与查询的其余部分无条件相关。这意味着您要从任何部门提取员工记录。

最后,我认为养成使用表别名的习惯是一个好习惯。它不仅使您的代码更短,而且更高级的查询通常会从同一个表的多个实例中提取,并且别名可以明确您指的是表的哪个实例。

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';
于 2013-04-11T03:45:49.660 回答
2

尝试这个

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'

您将需要 EmpDept 表上与 Departments 表匹配的 ID。

于 2013-04-11T03:49:29.257 回答
1

FROM如果访问包含多个连接,则访问需要在子句中使用括号。作为第一步,在 Access 查询设计器中尝试这样的查询。

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';

我认为我正确放置了括号;您可以在查询设计器中确认。但是,我对第二个ON条款感到困惑。

ON Departments.[Department Number] = '2'

该子句不引用联接“左侧”的任何字段。我不明白它应该完成什么,我不确定数据库引擎是否会在那里做你想要的。

于 2013-04-11T19:15:04.840 回答
1
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

squery中有一个小语法错误,最好用列名连接表并在where子句中提及列值条件。

于 2013-04-11T03:50:15.867 回答
0

尝试这个:

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'

如果您对子查询有兴趣,则需要给它起别名并确保它自己形成一个完整的查询:

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'
于 2013-04-11T14:31:58.330 回答