0

Hi i have queries that I use to get single result. How could I do this with just one query?

First I ask

SELECT "userId" FROM "Devices" WHERE "deviceNumber" = '0001563055080020'

Then save UserId and ask following another table

SELECT "email" FROM "Users" WHERE "id" = '1'

Any ideas?

1
  • Hint: INNER JOIN. Commented Jan 16, 2018 at 19:29

3 Answers 3

1

NESTED QUERY

SELECT "email" 
FROM "Users" 
WHERE "id" IN (
    SELECT "userId" 
    FROM "Devices" 
    WHERE "deviceNumber" = '0001563055080020')

INNER JOIN

select u.email
from users u, devices d
where d.userid = u.id
and d.deviceNumber = '0001563055080020'
Sign up to request clarification or add additional context in comments.

Comments

0

this is called a join

here is one syntax

select email
from users u, devices d
where d.userid = u.id

Comments

0

I recommend using the new join syntax. I think it's cleaner and easier to read. One can immediatly see there's an inner join. Furthermore the where-clause can be used just for filtering.

select u.*
from users u
inner join devices d on u.id=d.user_id
where d.device_number='0001234';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.