Sql 如何从子查询访问列
我想从子查询中获取列Sql 如何从子查询访问列,sql,postgresql,join,Sql,Postgresql,Join,我想从子查询中获取列to_address。如何在postgresql中获取它 我尝试为子查询分配'AS',但它不起作用对所有表使用join,您不需要任何子查询 select u.phone, u.email , t.to_address (error from this) from user_accounts u where u.id in (select w.user_id from wallets w where w.id in
to_address
。如何在postgresql中获取它
我尝试为子查询分配'AS',但它不起作用对所有表使用join,您不需要任何子查询
select u.phone, u.email , t.to_address (error from this)
from user_accounts u
where u.id
in
(select w.user_id
from wallets w
where w.id
in
(
select t.wallet_id
from withdraws t
where t.to_address
in
('1F6o1fZZ7', 'pJDtRRnyhDN')))
您无法访问t.address
,因为中的列处于
状态。
我使用了
left
join,但它似乎是internal join
类型,因为您在('1F6o1fZZ7','pJDtRRnyhDN')中使用了filter,尽管在应用where条件后,它的行为也类似于internal join一个join返回一个由多个表中的数据构造的结果表。还可以使用子查询检索相同的结果表。子查询只是另一个SELECT语句中的SELECT语句
select u.phone, u.email , ww.to_address
from user_accounts u left join wallets w on u.id=w.user_id
left jon withdraws ww on w.id=ww.wallet_id
where ww.to_address in ('1F6o1fZZ7', 'pJDtRRnyhDN')
使用子查询无法实现您正在尝试的功能。当您需要来自不同表的记录,并且这些记录有一个唯一的公共列连接它们时,您应该使用连接
<强>有时(并非所有的情况)<代码> 可能导致性能问题,因此您应该考虑更多的了解不同类型的<代码>连接< <代码> >(<)/P>
检查链接以进行比较:
关于查询:
select u.phone, u.email , t.to_address (
from user_accounts u
INNER JOIN wallets w ON u.id= w.user_id
INNER JOIN withdraws t ON t.wallet_id =w.id
where t.to_address in ('1F6o1fZZ7', 'pJDtRRnyhDN')
您引用了一个SQL Server性能问题,但此问题被标记为Postgres。此外:并非每个IN条件都可以重写为联接。这是两件不同的事情
SELECT
u.phone, u.email , t.to_address (error from this)
FROM
user_accounts u
INNER JOIN wallets w ON u.id = w.id
INNER JOIN withdraws t ON t.wallet_id = w.id
WHERE
t.to_address IN ('1F6o1fZZ7', 'pJDtRRnyhDN')