MySQL 表增删改操作
标签(空格分隔): MySQLday02 表增删改操作
添加记录 insert into
INSERT INTO table_name [(field1,field2...fieldn)]
VALUES (value1,value2...valuen);
#commoditytype 数据加入三条数据
insert into commoditytype (ct_id,ct_name)
values (1,'玩具'),(2,'文具'),(3,'书籍');
注意
- 添加记录用以上代码时注意对于中文字符的书写
- 对于属性需要按照标的结构顺序书写
- 属性和值需要一一对应,值得类型为定义的类型
- 用""表示值 而字段不需要进行修饰但是为保留字时修饰``
删除表记录
DELETE FROM table_name WHERE CONDITION;
delete from commoditytype where ct_id=3;
注意
- 删除语句一定要写删除条件,否则整张表删除!!!
修改原来数据
UPDATE table_name SET field1=value1,field2=value2,...fieldn=valuen WHERE CONDITION;
注意
- 一定必须不能忘了条件语句,否则整张表数据将被 修改成一样的!!!
单表查询
SELECT field1,field2, ... ,fieldn FROM table_name;
DISTINCT关键字去掉重复数据
SELECT DISTINCT field1,field2, ... FROM table_name;
select distinct c_type from commodity;
当输出其他的记录信息时输出的是符合条件的第一条信息
select语句中where的作用
- 带关系运算符和逻辑运算符的条件数据查询
select c_name as 商品名称,c_outprice as 商品售价,c_inprice as 商品进价 , c_outprice-`c_inprice` as 商品利润
from commodity;
| 乐高玩具-快乐家庭 | NULL | 50 | NULL |
当null参与运算的时候,结果也为null
- 带BETWEEN AND关键字的条件查询语句;
select c_name,c_inprice
from commodity
where (c_inprice between 10 and 100) and c_type=3;
包括between两端的数组
NOT BETWEEN AND
select c_name,c_inprice
from commodity
where c_inprice not between 10 and 100;
不包括两端的数字
- 带IS NULL关键字的条件查询语句;
SELECT field1 FROM table_name WHERE field2 IS [not] NULL;
判断选择记录是否为空
- 带IN关键字的条件查询语句;
select c_name,c_inprice
from commodity
where c_inprice in (10,20,30,40,50);
选择c_inprice只能为()内的元素的记录
select c_name,c_inprice from commodity
where c_inprice not in (10,20,30,40,50);
选择输出c_inprice只能不为()内的元素的记录
- 带LIKE关键字的条件查询语句
select c_name from commodity
where c_name like '%玩具%';
存在_ 和 %两种通配符
_代表着任意只用字符%代表着任意多个字符
当like ‘玩具’ 等同于 = ‘玩具’
like ‘%’ 等同于 全部查询
ORDER BY
是对一个字段进行分类并进行刷选处理
SELECT field1,field2,... FROM table_name WHERE CONDITION
ORDER BY fieldn [ASC|DESC];
两种排列方式
- order 为字典排序
- ASC 代表着升序 默认可以不写
- DESC 代表着降序
limit
选择输出多少条数据
select * from table limit 2; 只输出前2行;
select * from table limit 0,5;从第0行开始,输出5行;
select * from table order by sal limit 1;按照sal列升序排序
limit引用在分页查询
limit的查询方式是一句一句的查询当数据比较少时不影响时间
而当select * from table limit 1000000000,5;时会影响数据库的运行时间所以加入了sql优化
聚合函数
COUNT()函数: | 统计记录数; |
---|---|
AVG()函数: | 求平均值; |
SUM()函数: | 求和; |
MAX()函数: | 求最大数; |
MIN()函数: | 求最小数; |
select c_type,count(*) as '数量',min(c_inprice),max(c_outprice),avg(c_outprice)
from commodity group by c_type;
如下表
| sum(c_inprice) | avg(c_inprice) | max(c_outprice) |
|—|
| 2448 | 116.5714 | 3000 |
| 600 | 33.3333 | 460 |
| 1053 | 52.6500 | 400 |
注意
- 聚合函数(field) 不计算field字段为null
- count和其他聚合函数的区别
当没有结果的时候 其他函数返回null count返回0 可以用来判断用户的登入使用时不会造成空指针异常
having
select c_type, sum(c_inprice),avg(c_inprice),max(c_outprice)
from commodity
group by c_type;
如下表
| c_type | sum(c_inprice) | avg(c_inprice) | max(c_outprice) |
|—|
| 1 | 2448 | 116.5714 | 3000 |
| 2 | 600 | 33.3333 | 460 |
| 3 | 1053 | 52.6500 | 400 |
筛选group by 的表
select c_type , avg(c_inprice)
from commodity
group by c_type
having avg(c_inprice)>100;
| c_type | avg(c_inprice) |
|—|
| 1 | 116.5714 |
注意
- 这个having中不需要出现在结果表中列是从5.7版本才开始支持的
- 生成的表并没有存在硬盘上而是在内存上