2011-03-05 22 views
1

我有這樣的查詢:如何獲得聯合操作後的記錄數量?

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命令的執行...

,我想下面的查詢,但它給出了一個錯誤..

「SELECT COUNT(*)(從books_group選擇作者1類別爲」CSE工會選擇author2從books_group where category ='cse'union select_3 author_3 from books_group where category ='cse')「

然後,如何獲得聯合操作後錄音機的數量。

+0

你能解釋一下你打算要檢索後的數據做什麼? [兩個查詢]大概你有一些類型的程序利用這些數據。最後一個查詢的目的是簡單地顯示用戶找到了多少結果? [編程語言等是幫助] – 2011-03-05 16:18:37

+0

什麼是錯誤?和你運行這個數據庫? – Sumit 2011-03-05 16:20:22

回答

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 
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