SQL的增删改查 的使用
SQL
查询
select 字段列表 from 表名;
select id as 序号,name as 姓名 from 表名;#给字段取别名,最终显示的是别名
select * from 表名; #查询所有的字段
查询单个字段 不重复记录 distinct
select distinct 字段名 from 表名;
where 后边 可以接的条件
= 等于
> 大于
< 小于
>= 大于等于
<= 小于等于
!= 不等于
and 并且
or 或者
SELECT f_id as 序号,f_name as 名称,f_price as 价格 FROM fruits where f_price>10 and f_name='banana';
模糊查询
select * from 表名 where 字段名 like 'n%';
select * from 表名 where 字段名 like '%n%';
select * from 表名 where 字段名 like '%n';
查询字段是否允许为空
`select 字段名 from where 字段名 is not null;
`select 字段名 from where 字段名 is null;
排序 order
asc 为正序 ,默认
desc 为降序
select * from fruits where f_name is not null order by f_price; #默认是正序
select * from fruits where f_name is not null order by f_price asc; #跟上面效果一样
select * from fruits where f_name is not null order by f_price desc;#降序排列
多字段排序
select * from 表名 where 字段名 is not null order by 字段名 asc,字段名 desc;
如果第一个字段已经将结果排好 那么第二个字段 会被忽略
s_id的值 多个数据行是也一样的 那么 继续按照第二个字段进行排序
限制结果集 limit
select * from 表名 where 字段 is not null order by 字段名 asc,字段名 desc limit 10; #只显示符合结果的前十个
聚合函数
函数 | 说明 |
---|---|
sum | 求和 |
count | 统计 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
select 函数名(字段名) from 表名;
mysql> select max(f_price) from fruits;mysql> select min(f_price) from fruits;mysql> select sum(f_price) from fruits;mysql> select avg(f_price) from fruits;
分组
select 字段名 from 表名 group by 字段;
select 字段,avg(字段) from 表名 group by 字段名;
按照字段字段进行分组 ,然后列出每个每个分组的平均值
group_concat
separator 指定连接符
mysql> select s_id,group_concat(f_price separator '-') as f_price from fruits group by s_id;
+------+------------------+
| s_id | f_price |
+------+------------------+
| 101 | 10.20-5.20-3.20 |
| 102 | 5.30-11.20-10.30 |
| 103 | 2.20-9.20 |
| 104 | 5.50-6.40-7.60 |
| 105 | 2.60-11.60-8.20 |
| 106 | 15.60 |
| 107 | 3.60-3.60 |
+------+------------------+
7 rows in set (0.00 sec)mysql> select s_id,group_concat(f_price order by f_id separator '-') as f_price from fruits group by s_i
id;
+------+------------------+
| s_id | f_price |
+------+------------------+
| 101 | 5.20-10.20-3.20 | #这三个值 按照 f_id 进行正向排序
| 102 | 11.20-10.30-5.30 |
| 103 | 2.20-9.20 |
| 104 | 7.60-6.40-5.50 |
| 105 | 8.20-2.60-11.60 |
| 106 | 15.60 |
| 107 | 3.60-3.60 |
+------+------------------+
7 rows in set (0.00 sec)mysql> select s_id,group_concat(distinct f_price order by f_id separator '-') as f_price from fruits grooup by s_id;
+------+------------------+
| s_id | f_price |
+------+------------------+
| 101 | 5.20-10.20-3.20 |
| 102 | 11.20-10.30-5.30 |
| 103 | 2.20-9.20 |
| 104 | 7.60-6.40-5.50 |
| 105 | 8.20-2.60-11.60 |
| 106 | 15.60 |
| 107 | 3.60 | #相同的只留下了 一个
+------+------------------+
7 rows in set (0.00 sec)
在分组的结果上进行统计
mysql> select s_id,count(f_price) as 数量 ,group_concat(f_price separator '-') as 价格列表 from fruitsgroup by s_id;
+------+--------+------------------+
| s_id | 数量 | 价格列表 |
+------+--------+------------------+
| 101 | 3 | 10.20-5.20-3.20 |
| 102 | 3 | 5.30-11.20-10.30 |
| 103 | 2 | 2.20-9.20 |
| 104 | 3 | 5.50-6.40-7.60 |
| 105 | 3 | 2.60-11.60-8.20 |
| 106 | 1 | 15.60 |
| 107 | 2 | 3.60-3.60 |
+------+--------+------------------+
7 rows in set (0.00 sec
Copy
分组的结果上进行统计 然后再过滤 having
mysql> select s_id,count(f_price) as 数量 ,group_concat(f_price separator '-') as 价格列表 from fruits
group by s_id having 数量=1;
+------+--------+--------------+
| s_id | 数量 | 价格列表 |
+------+--------+--------------+
| 106 | 1 | 15.60 |
+------+--------+--------------+
1 row in set (0.00 sec)
多表联合查询
内连接
select 表1.字段名,表2.字段名,表n.字段名 where 条件 ;
外连接
左连接
select 表1.字段,表2.字段 from 表1 left join 表2 on 条件;
右连接
select 表1.字段,表2.字段 from 表1 right join 表2 on 条件;
多表查询
select 表1.字段1,表n.字段 from 表名1,表名2,表3 where 表1.字段=表2.字段 and 表2.字段=表3.字段;
子查询
mysql> select * from users where uid in (1,2,3,5);
+-----+--------------+----------+
| uid | username | password |
+-----+--------------+----------+
| 1 | 迪丽热巴 | 123456 |
| 2 | 古力娜扎 | 654321 |
| 3 | 热依扎 | 123123 |
| 5 | 珍塔玛莎 | 111111 |
+-----+--------------+----------+
4 rows in set (0.06 sec)mysql> select * from users where uid in (select uid from order_goos);
+-----+--------------+----------+
| uid | username | password |
+-----+--------------+----------+
| 1 | 迪丽热巴 | 123456 |
| 2 | 古力娜扎 | 654321 |
| 3 | 热依扎 | 123123 |
| 5 | 珍塔玛莎 | 111111 |
+-----+--------------+----------+
4 rows in set (0.00 sec)
union union all
两张没有关联的表 字段内容一样 可以放在一起显示 用union或者union all
union 是 union all 的去重结果
mysql> select uid from order_goos union select uid from users;
+-----+
| uid |
+-----+
| 2 |
| 1 |
| 3 |
| 5 |
| 8 |
| 4 |
+-----+
6 rows in set (0.00 sec)mysql> select uid from order_goos union all select uid from users;
+-----+
| uid |
+-----+
| 2 |
| 1 |
| 3 |
| 5 |
| 2 |
| 1 |
| 8 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+-----+
12 rows in set (0.00 sec)mysql> select uid,name from order_goos union all select uid,username from users;
+-----+--------------+
| uid | name |
+-----+--------------+
| 2 | 苹果鼠标 |
| 1 | skii精华 |
| 3 | 乳霜 |
| 5 | 寇驰包包 |
| 2 | 阿玛尼 |
| 1 | iPhone12 |
| 8 | 平板电脑 |
| 1 | 迪丽热巴 |
| 2 | 古力娜扎 |
| 3 | 热依扎 |
| 4 | 蒙娜丽莎 |
| 5 | 珍塔玛莎 |
+-----+--------------+
12 rows in set (0.00 sec)
修改update
update 表名 set 字段=值 where 条件;
删除 delete
drop table 表名 #删除表结构和数据
delete from 表名 #删除整个表的内容
delete from 表名 [where 条件] #根据条件删除指定内容
SQL的增删改查 的使用相关推荐
- SQL(一) —— 增删改查
SQL基础模块 SQL(一) -- 增删改查 一.初识数据库 1.1 DBMS的种类 1.2 RDBMS的常见系统结构 1.3 SQL 二.增删改查 2.1 数据类型 2.2 数据库操作 2.3 数据 ...
- php pdo 更新sql语句,增删改查sql语句及PDO数据库连接操作流程实例-2019年7月24日...
一.增删改查sql语句语法: 增: INSET INTO `表名` SET `字段名`=值 (也可写别名用,值前面加冒号->:别名): 删: DELETE FROM `表名` WHERE `字 ...
- Web SQL介绍,web sql中增删改查、数据存储位置、运行脚本、建表建库
Web SQL: Web SQL是一种简单的存在服务器中的数据库,运行在javascript脚本中,其数据储存在计算机Application中Web SQL中. web sql提供了三个核心方法: 1 ...
- SQL语句增删改查公司-员工3表典型案例
基础原生SQL增删改查,一般公司业务表,必知必会基础流程! 程序开发之大法,从数据库开始,不管是sqlserver,Oracle数据库还是其他MySQL等等的数据库.都是从写SQL语句开始.需要您必知 ...
- SQL语句(增删改查)
个人感觉SQL语句不用刻意的去记忆,多使用,用到的时候忘记了就查找一下,使用的频繁,自然就记住了. SQL语句使用:数据库操作SQL.数据表操作SQL.数据记录操作SQL (一)数据库SQL 1.创建 ...
- MYSQL中常用的SQL语句(增删改查)
MYSQL中常用的SQL语句 一.增删改查 1.新增 指定字段插入: INSERT INTO <表名> (<字段1>, <字段2>, <字段3>) VA ...
- MySQL数据库安装以及SQL基本增删改查
目录 1.介绍下数据库 1.1.DB概念和特点 1.2.常见DB软件 1.3.数据库结构 2.MySQL安装与使用 2.1.安装 2.2.卸载 2.3.MySQL服务启动与关闭 2.4.MySQL登录 ...
- sql入门--增删改查
参考学习 https://www.lintcode.com/course/list/?tagId=864&type=1&chargeStatus=-1 以下是我的学习笔记. 增删改查 ...
- 基本sql语句--增删改查
增 insert into 表名 values (值1,值2...): 也可以指定所要插入的列: insert into 表名 (列1,列2...) values (值1,值2...); 注:一般My ...
- 数据库知识点+sql语句增删改查(详解)
# 数据库相关SQL 1.查询所有数据库 - 格式: show databases; 2.创建数据库 - 格式: create database 数据库名 charset=utf8/gbk; - 举例 ...
最新文章
- 41款实用工具,数据获取、清洗、建模、可视化都有了
- Hhadoop环境部署
- RequestResponse(laravel)
- Windows Embedded Compact 2013升级:VS2013也能编译
- 1000个摄像头的网络怎么搭建?为什么500个就卡的不行?
- c++写一个类后编译发现class重定义
- 两个摄像头合成一路_64个高空抛物摄像头安装到位 同德社区居民双手点赞
- csdn怎么添加好友
- 最近抖音超火的60帧高清视频制作教程
- Eclipse 无法查看第三方jar包文件源代码解决方法(转载https://www.cnblogs.com/1995hxt/p/5252098.html自己备用)
- [转]C#中的global关键字(global::)
- 哔哩哔哩视频音频下载器批量下载器
- 上|中国股市九大伪元宇宙概念股
- Tailscale组成局域网(以文明6联机为例)
- 数据挖掘实战应用案例精讲-【概念篇】数据湖(补充篇)(Data Lake )
- 【电力系统经济调度】多元宇宙算法求解电力系统多目标优化问题附Matlab
- c/c++中 运算符 左结合与右结合
- 为触屏手机而设计系列1——拇指操作的“热区/死角”与“控件尺寸”
- 手把手搭建SSM框架
- 汽车文化-实用与实在-车身结构