2014-01-20 129 views
0

我正在查找MySQL數據庫中兩列的統計數據。對於單個列,我使用下面的查詢....查詢MySQL數據庫中兩列的統計數據

SELECT fname, count(fname) as nameCount, count(fname)/
(SELECT count(fname) FROM user_detail)*100 as percent 
FROM user_detail GROUP BY fname order by percent desc; 

這是合作得非常好,例如,上面的變量是名字「FNAME」,返回的第一個名字,計數相同的名字,以及名字在數據庫中表示的百分比。

我的下一步是比較名字與州,例如,我想看看有多少人在'佛羅里達州'有'Jeffrey'這個名字,但我不確定如何寫這個查詢,任何想法,想法或建議是非常讚賞的。

+0

SELECT COUNT(*)FROM user_detail WHERE FNAME = '傑弗裏' 和狀態= '佛羅里達' – Strawberry

回答

1

下面的查詢將給你按狀態計數,但是將爲整個表計算百分比。

SELECT fname, count(fname) as nameCount, count(fname)/
    (SELECT count(fname) FROM user_detail)*100 as percent 
FROM user_detail GROUP BY fname, state order by percent desc; 

對於由國家百分比試試這個

SELECT fname, count(fname) as nameCount, count(fname)/
    (SELECT count(fname) FROM user_detail as u WHERE u.state = d.satte)*100 as percent 
FROM user_detail as d GROUP BY fname, state order by percent desc;