我希望在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',它返回第一个名字,相同名字的数量,以及名字在数据库中代表的百分比。
我的下一步是将名字与州名进行比较,例如,我想知道有多少人在“佛罗里达州”中拥有名字'杰弗里',但我不知道如何撰写该查询,任何想法,非常感谢,想法或建议。
答案 0 :(得分: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;