0
我想從兩個表中獲得結果,這兩個表按照一個選擇中的相同規則分組在一起。連接來自兩個sql表的結果
我有表1
create table person AS
id INTEGER,
gender INTEGER,
state VARCHAR2
name VARCHAR2
surname VARCHAR2
表2
create table sampletest as
person_id FOREIGN KEY To person.id
result INTEGER
表3
create table examtest as
person_id FOREIGN KEY to person.id
examresult INTEGER
我謹狀態得到這個輸出
組|按性別分組|計數(檢驗> 0)|數(結果> 0,並導致< 4)
我想是這樣的
select state,gender,count(e.examresult),count(s.result) where
p.id=s.person_id and p.id=e.person_id and
s.result>0 and s.result<4 and
e.examresult>0 group by state,gender
,但我得到的結果依賴於對方。我如何獲得獨立的結果到一個選擇?
你能幫助更多一點,我需要狀態數0,即不要有e.examresult由0,1,2,3,4分組性別,5 – damir 2010-11-18 19:22:57
從性能角度來看,相關子查詢是一個糟糕的解決方案。這絕不應該是你的第一選擇。 – HLGEM 2010-11-18 21:44:52
我寫這樣的查詢,有時長達數百行,而且他們通常勝過其他解決方案。這可能取決於您使用的數據庫。使用Oracle時,經驗法則是查詢看起來越糟糕,通常執行得越好。但是,在其他答案中的左側加入可能會很好,甚至更好。 – GolezTrol 2010-11-19 08:28:29