Select in another Mysql table

Asked

Viewed 289 times

2

Any hint as to how I can select all clients of a particular user? I’m not really understanding the logic of how I should do the query.

MySQL Table

  • 1

    I think what you’re looking for is something like select * from clients where user_id = id_do_seu_usuario. Is relatively simple

  • 1

    Cool guy, I did the way you said I took the id of the logged User in the session and played in the query, returned the desired result, I thought it would be more complex and necessary to use joins.. VLW!

2 answers

2

By merging you will have the list of all the names of the clients table listed 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)

2


To bring only the customer data, based on a user ID (since you have the user reference user_id in the client table), da para fazer assim:

SELECT * FROM clients
WHERE user_id = 123456

But if you want to bring in customer data And user data, add Join to the query.

SELECT * FROM clients
JOIN users ON users.id = clients.user_id
WHERE clients.user_id = 123456

Browser other questions tagged

You are not signed in. Login or sign up in order to post.