工会运作后如何获得记录数量?

时间:2011-03-05 16:13:46

标签: sql database union

我有这样的查询:

select author1 
from books_group 
where category='cse' 
union 
select author2 
from books_group 
where category='cse' 
union 
select author3 
from books_group 
where category='cse'

上面的查询联合了来自三个选择命令的所有记录..

我的任务是计算执行上面的sql命令后我们拥有的记录数...

我尝试以下查询,但它给出了错误..

“选择count(*)from(从books_group中选择author1,其中category ='cse'union select author2 from books_group where category ='cse'union select author3 from books_group where category ='cse')”

然后,如何获得联合操作后的重复次数.. ???

2 个答案:

答案 0 :(得分:4)

试试这个:

    select count(*) from 
(select author1 from books_group where category='cse' 
union 
select author2 from books_group where category='cse' 
union 
select author3 from books_group where category='cse')A

答案 1 :(得分:4)

你很接近,你需要为你的子选择指定一个别名:

select
    count(*)
from
    (
    select author1 from books_group where category='cse' union
    select author2 from books_group where category='cse' union
    select author3 from books_group where category='cse'
    ) a