Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting records in SQL based on another table's contents

Tags:

sql

I'm a bit new to SQL and have trouble constructing a select statement. I have two tables:

Table users
    int id
    varchar name

Table properties
    int userID
    int property

and I want all user records which have a certain property. Is there a way to get them in one SQL call or do I need to first get all userIDs from the properties table and then select each user individually?

like image 450
Steve Avatar asked Nov 09 '09 19:11

Steve


1 Answers

Use a JOIN:

SELECT U.id, U.name, P.property FROM users U
INNER JOIN properties P ON P.userID = U.id
WHERE property = 3
like image 191
Gary McGill Avatar answered Nov 15 '22 10:11

Gary McGill