带括号FROM子句的查询中的语法错误(缺少运算符)

时间:2013-04-11 03:40:17

标签: c# asp.net sql ms-access join

我正在使用带有访问数据库的asp.NET和C#来创建Web应用程序。

我当前的问题是SQL语句。在项目中,我已成功使用字符串值来构建和存储查询。因为它存储在一个字符串中,所以看起来有点难看。问题在于我写的最后一个查询。它有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 Departments.[Department Number] = '2'
)
WHERE Departments.[Campus]='Clarion';

这是非常丑陋的,我知道..我希望,因为这是一个语法错误,它不会太难。

此查询旨在返回员工的姓名,电子邮件和部门。 2用c#代码给出,并在代码的前面确定,但它代表某个部门。 empDept表位于Departments表和employee表之间,因此员工可以在多个部门中。

非常感谢任何帮助。感谢

5 个答案:

答案 0 :(得分:5)

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

另外,根据你的描述,我会编写查询以匹配连接中的部门与EmpDept表中的内容,然后使用WHERE子句过滤到dept'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';

答案 1 :(得分: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。

答案 2 :(得分: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子句中提及列值条件。

答案 3 :(得分: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'

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

答案 4 :(得分: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'