I have two tables, user and userfield
I wanted to select a few datapoint from both tables, searching based on the username field in the user table. The user table also contains a userid, and so subsequently there are a few datapoints I wanted to extract from the userfield table using the userid datapoint
Here is an example of the data
Table: User
++ user ++ userid ++ avatar
++ Mark ++ 12345 ++ 15
++ John ++ 54321 ++ 25
Table: Userfield
++ userid ++ location++ sex
++ 12345 ++ USA ++ Male
++ 54321 ++ Canada ++ Male
Is it possible to select the user, userid and location at the same time with a single query based on only a provided username?
Tahnks!
You're looking for a very simple SQL concept call the the JOIN. You want to join the two tables on the userid field:
SELECT user, avatar, location, sex
FROM User JOIN Userfield USING (userid)
This will select all columns from both tables where the userids match.
To filter by particular usernames, use a WHERE
clause:
WHERE user = 'Mark'
WHERE user IN ('Mark', 'John')
Is this all you're looking for -- a basic INNER JOIN
:
SELECT *
FROM User U
INNER JOIN UserField UF ON U.UserId = UF.UserId
WHERE U.User = 'UserName'