dongtong7990 2016-03-28 23:35
浏览 19
已采纳

关系表DB

I have two tables:

Users Table:

ID    | Name  | UserGroupID  |

1   | John  | 2

2   | Sam    | 11

3   | Kiddo   | 2

4   | Sony   | 3

5   | Jabeen | 1

UsersMachine Table:

ID  | MachineID | EmpID
1  | 1  | 1

2  | 2  | 1

3  | 1  | 2

4  | 2  | 2

5  | 2  | 4

6  | 3  | 5

i m looking at single sql to repeat all Users by filtering FROM UserMachine WHERE MachineID = 2 ALSO FROM Users WHERE UsersGroupID IN (2,11) with these conditions

I m looking at following reasults:

MachineID | UsersName | UsersGroupID 

2     | John  | 2

2    | Sam  | 11
  • 写回答

1条回答 默认 最新

  • dqb77047 2016-03-28 23:42
    关注

    Check out joins. They allow us to associate data from two different tables in a single query:

    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)
    

    This is probably the best post (visually) I have seen that help me with joins: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?