0

So we have 2 tables

users:

id | username | email
1  | testuser | test@test.com
2  | dirk     | test2@test2.com

data:

id | data         | username
1  | somedata     | dirk
3  | somemoredata | testuser

I want to select the username of the data table with the id 3 and then select his email. Which is the nicest way to do this with an inner join?

2

You can get the result by joining your two tables on the username column, then you can use a WHERE clause to filter for the row with id = 3:

select d.id, d.username, u.email
from data d
inner join users u
  on d.username = u.username
where d.id = 3;

See SQL Fiddle with Demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.