Mohammed Kumel
Mohammed Kumel

Reputation: 151

Relational Table 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

Upvotes: 0

Views: 32

Answers (1)

Erik Ahlswede
Erik Ahlswede

Reputation: 2155

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/

Upvotes: 1

Related Questions