mani
mani

Reputation: 1044

how to getting number of records after union operation..?

I have a query like this:

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'

the above query union the all the records that are comes from three select commands..

my task is to count the number of records we have after the execution of above sql command...

and i trying the below query but it gives an error..

" 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') "

then, how to get the number of recors after the union operation..???

Upvotes: 1

Views: 1834

Answers (2)

vcsjones
vcsjones

Reputation: 141678

You were close, you need to specify an alias for your subselect:

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

Upvotes: 4

Radu Caprescu
Radu Caprescu

Reputation: 993

Try this:

    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

Upvotes: 4

Related Questions