2017-04-06 50 views
0

我有这个疑问,2列规定,但它不工作没有列名在“t”

select t.ACCOUNT_ID 
from 
    (select 
     a.ACCOUNT_ID, count(a.ACCOUNT_ID) 
    from 
     EMPLOYER a, EMPLOYER b 
    where 
     a.main_id = b.main_id 
    group by 
     a.MAIN_ID, a.account_id 
    having 
     count(a.account_id) > 1) t 

我试图找到重复,想从 子查询获取的唯一ACCOUNT_ID

+0

[不良习惯踢:使用旧样式的JOIN(http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to- kick-using-old-style-joins.aspx) - 在ANSI - ** 92 ** SQL标准中,旧式*表格样式的旧式*逗号分隔列表已替换为* proper * ANSI'JOIN'语法** 25年前**),其使用是不鼓励的 –

回答

1
select t.ACCOUNT_ID from (select 
a.ACCOUNT_ID,count(a.ACCOUNT_ID) as cnt 
from EMPLOYER a,EMPLOYER b 
where a.main_id = b.main_id 
group by a.MAIN_ID,a.account_id 
having count(a.account_id) > 1)t 

如果您打算从子查询中进行选择,您应该至少命名每列的第一个实例。

新增as cnt

COUNT(a.account_id)