| 您的当前位置:首页 --> MYSQL教程 --> 详解MySQL中的分组查询与连接查询语句 |
| MYSQL教程 详解MySQL中的分组查询与连接查询语句 |
| 浏览次数:1136 关键词 ( ) |
| 查看使用该CPU的产品 查看CPU天梯 |
| CPU型号:详解MySQL中的分组查询与连接查询语句 |
| 主频:Ghz |
| 睿频:Ghz |
| 核心数:个 |
| 不支持超核心 |
| 制作工艺: |
| 插槽类型: |
| 功耗:0W |
| L3缓存:0MB |
| 支持最大内存: 0GB |
| CPU详细参数 |
|
分组查询 group by 1)单独使用 select * from employee group by sex; 将只显示男女两条记录。 2)与group_concat()函数一起使用 select sex,group_concat(name) from employee group by sex; 显示结果中“女”会显示所有sex为“女”的名字name sex | group_concat(name) 女 | 小红,小兰 男 | 张三,王五,王六 3)与集合函数一起使用 select sex,count(sex) from employee group by sex; 结果: sex | count(num) 女 | 1 男 | 3 count()为计算个数的方法。 4)与having一起使用 select sex,count(sex) from employee group by sex having count(sex) >= 3; 结果: sex | count(sex) 男 | 3 “having条件表达式”作用于分组后的记录。 5)按多字段进行分组 select * from employee group by d_id,sex; 查询结果先按d_id分组,再按sex进行分组 6) 与with rollup一起使用 select sex,count(sex) from employee group by sex with rollup; 结果: sex | count(sex) 女 | 1 男 | 5 null | 6 如果是字符串的话,比如姓名就会生成“张三,李四,王五”这种类型的结果,即name总和。 连接查询 1)内连接查询: 复制代码 代码如下: select num,name,employee.d_id,age,d_name from employee,department where employee.d_id = department.d_id
2)外连接查询 复制代码 代码如下: select num,name,employee.d_id,age,d_name from employee left join department on employee.d_id = department.d_id; 右连接查询:
select count(*) from employee; 与group by一起使用 select d_id,count(*) from employee group by d_id; 上述语句会先分组后统计。 2) sum()函数 select num,sum(score) from grade where num= 1001; select num,sum(score) from grade group by num; sum()只能计算数值类型字段。 select avg(age) from employee; select course,avg(score) from group by course; 4)max(),min()函数 |
| 下一个产品 SQL计算timestamp的差值的方法 上一个产品 linux下安装升级mysql到新版本(5.1-5.7) |