![](https://img.haomeiwen.com/i12079082/12ecceab6d8310b9.png)
Delete from student where id in(3,6,7,9,10);
删除id是3,6,7,9,10的数据。
Truncate table student
Delete from student where i=1(1=1表示ture,相当于不写,也没有关系)
清空数据表,只保留表的结构,定义表
这两个删除的区别是前者速度快,后者慢些。Delete会触发日志恢复操作和删除触发器
Delete是一个一个删除,但是truncate是一起全部删除。
Delete 重新增加数据,id还是重以前的排序开始,比如删掉数据之前已经到了200,这次新增数据就从201开始编号,但是truncate依旧从1开始编号,所以如果想让表格回到初始化的花truncate比较好。
![](https://img.haomeiwen.com/i12079082/bba463b4faf6f632.png)
![](https://img.haomeiwen.com/i12079082/81e0e37416dbacd4.png)
![](https://img.haomeiwen.com/i12079082/1e45034ef60ea627.png)
![](https://img.haomeiwen.com/i12079082/30c189791d7ff250.png)
Count(*)对表中行的数目进行计数,不管表列中包含的是空值还是非空值;
Count(column)对特定列中具有值的行进行计数,忽略null值。
Max和min 能返回文本列中的最大值,一般按一定顺序排序的话,max返回最后一列,min返回第一列。
Avg(distinct price)平均值只考虑各个不同的价格。
select vend_id,count(*)as num_prods fromproducts group by vend_idwithrollup;
with rollup对所有种类进行了汇总。
Where 过滤行,having 过滤分组,所以group by 后面不用where 而用having.还有一种理解方法wherw过滤发生在分组前,但是having发生在分组后,where 排除的行不包括在分组中。
也有同时使用where和having 的情况,如
Select id,count(*) from products where price>10 group by id havingcount(*)>2;
Where过滤掉所有价钱小于10的,然后再根据id分组,having过滤掉计数为2或者以上的分组,如果没有where过滤会多出两个数据(一个供应商,销售的产品都在10元以下,但是种类超过2个,一个供应商3个产品,只有一个大于10元。)
Select 子句顺序
Select
From
Where
Group by
Having
Order by
limit
![](https://img.haomeiwen.com/i12079082/c6d375e4eb7f5526.png)
子查询:嵌套在其它查询里面的查询。
子查询总是从内向外处理,
Select name, state ,(select count(*) fromorder whereorder.id=customer.id)as orders from customers.
从customers表中检索客户列表。
对于检索出的每个用户,统计其在orders表中的订单数目。
![](https://img.haomeiwen.com/i12079082/f3b41a829dc6a3e4.png)
数值函数
![](https://img.haomeiwen.com/i12079082/33ff9e4d874df0c6.png)
![](https://img.haomeiwen.com/i12079082/27e6e27b3e6dd59b.png)
![](https://img.haomeiwen.com/i12079082/711f30ce7b054adc.png)
![](https://img.haomeiwen.com/i12079082/a1e8a9b622beca34.png)
网友评论