SQL 包含 3 个条件的语句
SQL Statement with 3 conditions
我有两个表:
Name Forename CostCentre
Max Meier 11111
Paul Peters 22222
Kai Green 11111
CostCentre departmentCostCentre
11111 HR
22222 IT
除此之外,我还有一个搜索字段和一个用于成本中心的组合框。
If I enter "a" in the searchfield and "11111" in cost centre, I'll get all records...
但我只想得到Max和Kai。这是我的 SQL 声明:
SELECT tbl_Employee.Name, tbl_Employee.Forename, tbl_Employee.CostCentre, tbl_Department.Department
FROM tbl_DepartmentINNER JOIN tbl_EmployeeON tbl_Department.CostCentre= tbl_Employee.CostCentre
WHERE tbl_Employee.Name Like "*a*" OR tbl_Employee.Forename Like "*a*"AND tbl_Mitarbeiter.CostCentre=44444;
我真的不知道错误在哪里....如果我删除名称或名字条件它工作正常,但两者我得到奇怪的结果...
SELECT
*
FROM
tbl_Employee
WHERE
tbl_Employee.Name LIKE '%a%'
AND tbl_Employee.CostCentre = 11111;
如果您希望成本中心条件始终适用并且名称条件适用于任一名称,则需要使用括号:
SELECT * FROM tbl_Employee
WHERE (tbl_Employee.Name Like 'a' Or
tbl_Employee.Forename Like 'a') And
tbl_Employee.CostCentre=22222;
否则,And
比 Or
绑定得更紧密,而你是说 或者 Name
条件必须匹配 or Forename
和 CostCentre
条件必须匹配。
你的问题确实已经在你的代码中包含了一些括号,我已经删除了这些括号。我不确定它们与什么有关。
基于更新的查询:
SELECT tbl_Employee.Name, tbl_Employee.Forename, tbl_Employee.CostCentre, tbl_Department.Department
FROM tbl_DepartmentINNER JOIN tbl_EmployeeON tbl_Department.CostCentre= tbl_Employee.CostCentre
WHERE
(
tbl_Employee.Name Like "*a*"
OR
tbl_Employee.Forename Like "*a*"
)
AND
tbl_Mitarbeiter.CostCentre=44444;
我有两个表:
Name Forename CostCentre
Max Meier 11111
Paul Peters 22222
Kai Green 11111
CostCentre departmentCostCentre
11111 HR
22222 IT
除此之外,我还有一个搜索字段和一个用于成本中心的组合框。
If I enter "a" in the searchfield and "11111" in cost centre, I'll get all records...
但我只想得到Max和Kai。这是我的 SQL 声明:
SELECT tbl_Employee.Name, tbl_Employee.Forename, tbl_Employee.CostCentre, tbl_Department.Department
FROM tbl_DepartmentINNER JOIN tbl_EmployeeON tbl_Department.CostCentre= tbl_Employee.CostCentre
WHERE tbl_Employee.Name Like "*a*" OR tbl_Employee.Forename Like "*a*"AND tbl_Mitarbeiter.CostCentre=44444;
我真的不知道错误在哪里....如果我删除名称或名字条件它工作正常,但两者我得到奇怪的结果...
SELECT
*
FROM
tbl_Employee
WHERE
tbl_Employee.Name LIKE '%a%'
AND tbl_Employee.CostCentre = 11111;
如果您希望成本中心条件始终适用并且名称条件适用于任一名称,则需要使用括号:
SELECT * FROM tbl_Employee
WHERE (tbl_Employee.Name Like 'a' Or
tbl_Employee.Forename Like 'a') And
tbl_Employee.CostCentre=22222;
否则,And
比 Or
绑定得更紧密,而你是说 或者 Name
条件必须匹配 or Forename
和 CostCentre
条件必须匹配。
你的问题确实已经在你的代码中包含了一些括号,我已经删除了这些括号。我不确定它们与什么有关。
基于更新的查询:
SELECT tbl_Employee.Name, tbl_Employee.Forename, tbl_Employee.CostCentre, tbl_Department.Department
FROM tbl_DepartmentINNER JOIN tbl_EmployeeON tbl_Department.CostCentre= tbl_Employee.CostCentre
WHERE
(
tbl_Employee.Name Like "*a*"
OR
tbl_Employee.Forename Like "*a*"
)
AND
tbl_Mitarbeiter.CostCentre=44444;