Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Multiple table select with condition

I have a noob question but rather a troublesome one for me. I am using SELECT on three tables the middle one of which is realtional (Holds relations - ID of user against ID of Place), the first is a table of users, the last of places. I have written this perfectly woking query

$query = "SELECT users.Username,usrxplc.User,places.Name
          FROM users,usrxplc,places
          WHERE usrxplc.Place=places.ID AND usrxplc.User=users.ID"

That spits out all places associated with all users. Fine, but I would like to limit it only to a certain user. Seems simple, but I am stuck.

like image 523
Whitewall Avatar asked May 15 '26 05:05

Whitewall


1 Answers

You use a WHERE clause to filter the results, so just add a clause for users.ID:

select users.Username,
    usrxplc.User,
    places.name
from users,
    usrxplc,
    places
where usrxplc.Place = places.ID
    and usrxplc.User = users.ID
    and users.ID = 123
like image 170
D'Arcy Rittich Avatar answered May 17 '26 01:05

D'Arcy Rittich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!