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?
Use a
JOIN
:You're looking to
JOIN
tables.Assuming the id and userID columns have the same meaning, it's like this:
Obviously it depends what flavour of RDBMS and TSQL you are using.
If there's only one property row per user you want to select on, I think this is what you want:
If you have multiple property rows matching "whatnot" and you only want one, depending your database system, you either want a left join or a distinct clause.
Check out the JOIN command. You could write a query like the following: