数据如下:
1. SEX EDUCATION EXISTING
1. 女 高中 新户
1. 女 本科 新户
1. 女 高中 旧户
1. 男 高中 新户
1. 女 高中 新户
1. 男 本科 新户
需要统计的结果:
条件是目前只知道表名,不知道具体的字段名,现在用的方法比较笨,先去information_schema获取所有的字段,再循环拼sql查询,不知道有没有更好的方法?
select 'sex',f.sex, count(f.sex) from table f group by f.sex
union
select 'education',f.education,count(f.education) from table f group by f.education
首先,查询表字段好像就是题主说的information_schema
然后
这种情况都是union all加存储过程解决的
http://www.cnblogs.com/summers/archive/2013/07/03/3170577.html
使用語句mysql distinct!!!