Postgres查询来自两个不同的表

时间:2018-01-16 19:27:16

标签: sql postgresql select-query

您好我有用于获得单个结果的查询。我怎么能用一个查询来做到这一点?

首先我问

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

然后保存UserId并询问另一个表

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

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

NESTED QUERY

select u.email
from users u, devices d
where d.userid = u.id
and d.deviceNumber = '0001563055080020'

INNER JOIN

{{1}}

答案 1 :(得分:0)

这称为连接

这是一种语法

card1 = random.randint(0, 3)
deck_of_cards = [A_hearts, 2_diamonds, Queen_hearts, 5_spades]
value = [11, 2, 10, 5]
def dealcards():
     print deck_of_cards[card1]
     print value[card1]
     print 'This is your first card and it's value.'

答案 2 :(得分:0)

我建议使用新的连接语法。我认为它更清洁,更容易阅读。人们可以立即看到内部联接。此外,where子句只能用于过滤。

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