Select in another MySQL table

2

Any tips on how I can select all clients for a particular user? I'm not really understanding the logic of how to do the query .

    
asked by anonymous 17.04.2017 / 19:40

2 answers

2

To bring only the customer data, based on a user ID (since you have the user_id user reference in the customer table), you can do this:

SELECT * FROM clients
WHERE user_id = 123456

But if you want to bring client data AND user data, add the join in the query.

SELECT * FROM clients
JOIN users ON users.id = clients.user_id
WHERE clients.user_id = 123456
    
17.04.2017 / 21:05
2

By joining you will have the list of all the names of the clients table that are in the users table.

SELECT clients.name 
FROM users INNER JOIN clients 
ON (users.id = clients.user_id)

If you want to bring all the columns, just replace name with *

SELECT clients.* 
FROM users INNER JOIN clients 
ON (users.id = clients.user_id)
    
17.04.2017 / 20:06