2012-09-28 64 views
1

我有一個包含兩個表的數據庫,我想通過一個查詢獲取這些表中的行數總和。 到目前爲止牛逼嘗試:如何使用一個查詢獲取數據庫上的總行數

SELECT (count(bill.*) + count(items.*)) as TTL FROM bill, items // Failed 
SELECT count(*) as TTL FROM bill, items // wrong total 
SELECT (count(bill.ID_B) + count(items.ID_I)) as TTL FROM bill, items // wrong total 
SELECT count(bill.ID_B + items.ID_I) as TTL FROM bill, items // return the biggest total 
+1

http://stackoverflow.com/questions/606234/select-count-from-multiple-tables –

回答

3

使用兩個子查詢:

select (select count(1) from bill) + (select count(1) from items); 
+0

@JDwyer請問你解釋爲什麼? – SIFE

+0

現在,數據庫可能會使這些操作相同。傳統的觀點是,選擇常數a.k.a 1或'a'比從表格中選擇值要快。請參閱:http://stackoverflow.com/questions/1221559/count-vs-count1 – JDwyer

相關問題