如何使用子查询连接两个表

时间:2017-11-27 09:23:38

标签: sql database oracle

我需要从表CUSTOMER获取客户名称,并从表ACCOUNT中获取余额。我加入两个表但无济于事。请注意,如果要连接两个包含相同列名的表,则必须使用对多个表中存在的列名的前缀引用,其中包含表名或表别名和句点(。),或者错误消息ORA- 00918列模糊定义将显示。

select c.cfirst, c.clast, a.balance
from customer c, account a
where c.social = a.social AND social in (select social from account where social in (select social
from trans where dotrans > '04/01/2016' and means_of_trans = 'credit'));

2 个答案:

答案 0 :(得分:0)

您必须为社交列添加alias名称。因为这两个表都有这个列。

试试这个:

select c.cfirst, c.clast, a.balance
from customer c, account a
where c.social = a.social AND a.social in (select social
                                         from account
                                         where social in (select social

                                                        from trans
                                                        where dotrans > '04/01/2016' and means_of_trans = 'credit')); 

答案 1 :(得分:0)

无需再次对帐户表进行子查询,因为您只需使用转换表中的社交值检查帐户表中的社交值。

  select c.cfirst, c.clast, a.balance
    from customer c, account a
    where c.social = a.social 
    AND a.social in ( select social
                      from trans where dotrans > '04/01/2016' 
                      and means_of_trans = 'credit'
                     );