我有两张桌子:
用户表:
ID | Name | UserGroupID |
1 | John | 2
2 | Sam | 11
3 | Kiddo | 2
4 | Sony | 3
5 | Jabeen | 1
UsersMachine表:
ID | MachineID | EmpID
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 2 | 2
5 | 2 | 4
6 | 3 | 5
我看着单个sql通过过滤重复所有用户
FROM UserMachine WHERE MachineID = 2
FROM Users WHERE UsersGroupID IN (2,11)
还有这些条件
我正在观察以下结果:
MachineID | UsersName | UsersGroupID
2 | John | 2
2 | Sam | 11
答案 0 :(得分:1)
查看联接。它们允许我们在单个查询中关联来自两个不同表的数据:
SELECT um.MachineId, users.Name, users.UsersGroupID FROM UsersMachine um
INNER JOIN Users users ON users.id = um.EmpID
WHERE um.MachineID = 2 AND users.UsersGroupID IN (2,11)
这可能是我见过的最好的帖子(视觉上)帮我加入: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/