程序员最实用的 SQL 语句收藏,看完这篇就够了


当前第2页 返回上一页

查询前几行

mysql> select id,name from users limit 2;
+----+--------+
| id | name |
+----+--------+
| 1 | steven |
| 2 | bill |
+----+--------+

查询从指定偏移(第一行为偏移为0)开始的几行

mysql> select id,name from users limit 2,3;
+----+--------+
| id | name |
+----+--------+
| 3 | 张三 |
| 4 | 李四 |
| 5 | 王五 |
+----+--------+

5.2.6 排序

# 正序
mysql> select distinct name from users order by name asc limit 3;
+--------+
| name |
+--------+
| bill |
| steven |
| 刘八 |
+--------+
# 倒序
mysql> select id,name from users order by id desc limit 3;
+----+--------+
| id | name |
+----+--------+
| 13 | 王五 |
| 12 | 李四 |
| 11 | 张三 |
+----+--------+

5.2.7 分组

增加城市字段

alter table `users` add `city` varchar(10) comment '用户所在城市' after `name`;
update `users` set `city`='旧金山' where `id`=1;
update `users` set `city`='西雅图' where `id`=2;
update `users` set `city`='北京' where `id` in (3,5,7);
update `users` set `city`='上海' where `id` in (4,6,8);
update `users` set `city`='广州' where `id` between 9 and 10;
update `users` set `city`='深圳' where `id` between 11 and 13;

按城市分组统计用户数

mysql> select city, count(name) as num_of_user from users group by city;
+-----------+-------------+
| city | num_of_user |
+-----------+-------------+
| 上海 | 3 |
| 北京 | 3 |
| 广州 | 2 |
| 旧金山 | 1 |
| 深圳 | 3 |
| 西雅图 | 1 |
+-----------+-------------+
mysql> select city, count(name) as num_of_user from users group by city having num_of_user=1;
+-----------+-------------+
| city | num_of_user |
+-----------+-------------+
| 旧金山 | 1 |
| 西雅图 | 1 |
+-----------+-------------+
mysql> select city, count(name) as num_of_user from users group by city having num_of_user>2;
+--------+-------------+
| city | num_of_user |
+--------+-------------+
| 上海 | 3 |
| 北京 | 3 |
| 深圳 | 3 |
+--------+-------------+

5.3 多表关联查询

5.3.1 准备数据

create table if not exists `orders`
(
 `id` int not null primary key auto_increment comment '订单ID',
 `title` varchar(50) not null comment '订单标题',
 `user_id` int not null comment '用户ID',
 `cretime` timestamp not null default current_timestamp comment '创建时间'
);
create table if not exists `groups`
(
 `id` int not null primary key auto_increment comment '用户组ID',
 `title` varchar(50) not null comment '用户组标题',
 `cretime` timestamp not null default current_timestamp comment '创建时间'
);
alter table `users` add `group_id` int comment '用户分组' after `city`;
insert into `groups` (`title`) values ('大佬'), ('萌新'), ('菜鸡');
insert into `orders` (`title`, `user_id`) values ('《大佬是怎样炼成的?》', 3), ('《MySQL 从萌新到删库跑路》', 6), ('《菜鸡踩坑记》', 9);
update `users` set `group_id`=1 where `id` between 1 and 2;
update `users` set `group_id`=2 where `id` in (4, 6, 8, 10, 12);
update `users` set `group_id`=3 where `id` in (3, 5, 13);

5.3.2 join

join

用于在多个表中查询相互匹配的数据。

mysql> select `users`.`name` as `user_name`, `orders`.`title` as `order_title` from `users`, `orders` where `orders`.`user_id`=`users`.`id`;
+-----------+--------------------------------------+
| user_name | order_title |
+-----------+--------------------------------------+
| 张三 | 《大佬是怎样炼成的?》 |
| 马六 | 《MySQL 从萌新到删库跑路》 |
| 杨九 | 《菜鸡踩坑记》 |
+-----------+--------------------------------------+

inner join

内部连接。效果与 join 一样 , 但用法不同,join 使用 where ,inner join 使用 on 。

mysql> select `users`.`name` as `user_name`, `orders`.`title` as `order_title` from `users` inner join `orders` on `orders`.`user_id`=`users`.`id`;
+-----------+--------------------------------------+
| user_name | order_title |
+-----------+--------------------------------------+
| 张三 | 《大佬是怎样炼成的?》 |
| 马六 | 《MySQL 从萌新到删库跑路》 |
| 杨九 | 《菜鸡踩坑记》 |
+-----------+--------------------------------------+

left join

左连接。返回左表所有行,即使右表中没有匹配的行,不匹配的用 NULL 填充。

```
mysql> select `users`.`name` as `user_name`, `orders`.`title` as `order_title` from `users` left join `orders` on `orders`.`user_id`=`users`.`id`;
+-----------+--------------------------------------+
| user_name | order_title |
+-----------+--------------------------------------+
| 张三 | 《大佬是怎样炼成的?》 |
| 马六 | 《MySQL 从萌新到删库跑路》 |
| 杨九 | 《菜鸡踩坑记》 |
| steven | NULL |
| bill | NULL |
| 李四 | NULL |
| 王五 | NULL |
| 肖七 | NULL |
| 刘八 | NULL |
| 郑十 | NULL |
| 张三 | NULL |
| 李四 | NULL |
| 王五 | NULL |
+-----------+--------------------------------------+

```

**right join**

右连接。和 left join 正好相反,会返回**右表**所有行,即使**左表**中没有匹配的行,不匹配的用 NULL 填充。
```sql
mysql> select `groups`.`title` as `group_title`, `users`.`name` as `user_name` from `groups` right join `users` on `users`.`group_id`=`groups`.`id`;
+-------------+-----------+
| group_title | user_name |
+-------------+-----------+
| 大佬 | steven |
| 大佬 | bill |
| 萌新 | 李四 |
| 萌新 | 马六 |
| 萌新 | 刘八 |
| 萌新 | 郑十 |
| 萌新 | 李四 |
| 菜鸡 | 张三 |
| 菜鸡 | 王五 |
| 菜鸡 | 王五 |
| NULL | 肖七 |
| NULL | 杨九 |
| NULL | 张三 |
+-------------+-----------+

```

**5.3.3 union**

union 用于合并两个或多个查询结果,合并的查询结果必须具有相同数量的列,并且列拥有形似的数据类型,同时列的顺序相同。


```sql
mysql> (select `id`, `title` from `groups`) union (select `id`, `title` from `orders`);
+----+--------------------------------------+
| id | title |
+----+--------------------------------------+
| 1 | 大佬 |
| 2 | 萌新 |
| 3 | 菜鸡 |
| 1 | 《大佬是怎样炼成的?》 |
| 2 | 《MySQL 从萌新到删库跑路》 |
| 3 | 《菜鸡踩坑记》 |
+----+--------------------------------------+
```

6. 函数

6.1 语法

**select function**(*column*) **from** *table_name*

6.2 合计函数(Aggregate functions)

合计函数的操作面向一系列的值,并返回一个单一的值。通常与 group by 语句一起用。

函数 描述 avg(column) 返回某列的平均值 count(column) 返回某列的行数(不包括 NULL 值) count(*) 返回被选行数 first(column) 返回在指定的域中第一个记录的值 last(column) 返回在指定的域中最后一个记录的值 max(column) 返回某列的最高值 min(column) 返回某列的最低值 sum(column) 返回某列的总和 6.3 标量函数(Scalar functions)

函数 描述 ucase(c) 转换为大写 lcase(c) 转换为小写 mid(c, start[, end]) 从文本提取字符 len(c) 返回文本长度 instr(c, char) 返回在文本中指定字符的数值位置 left(c, number_of_char) 返回文本的左侧部分 right(c, number_of_char) 返回文本的右侧部分 round(c, decimals) 对数值指定小数位数四舍五入 mod(x, y) 取余(求模) now() 返回当前的系统日期 format(c, format) 格式化显示 datediff(d, date1, date2) 日期计算

以上就是程序员最实用的 SQL 语句收藏看完这篇就够了的详细内容,更多关于程序员SQL 语句的资料请关注其它相关文章!


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...