文档加密软件排名(sql工具有哪些)
Excel是数据分析中最常用的工具 ,利用Excel可以完成数据清洗,预处理,以及最常见的数据分类,数据筛选,分类汇总,以及数据透视等操作,而这些操作用SQL一样可以实现。
SQL不仅可以从数据库中读取数据,还能通过不同的SQL函数语句直接返回所需要的结果,从而大大提高了自己在客户端应用程序中计算的效率。
1 重复数据处理
查找重复记录
select * FROM user Where (nick_name,password) in(select nick_name,password FROM user group by nick_name,password having count(nick_name)>1);
查找去重记录
查找id最大的记录
select * FROM user WHERE id in(select max(id) FROM usergroup by nick_name,password having count(nick_name)>1);
删除重复记录
只保留id值最小的记录
delete c1FROM customer c1,customer c2WHERE c1.cust_email=c2.cust_emailAND c1.id>c2.id;
delete FROM user Where (nick_name,password) in(select nick_name,password FROM(select nick_name,password FROM user group by nick_name,password having count(nick_name)>1) as tmp1)and id not in(select id FROM(select min(id) id FROM user group by nick_name,password having count(nick_name)>1) as tmp2);
2 缺失值处理
查找缺失值记录
select * FROM customerWHERE cust_email IS ;
更新列填充空值
update sale set city = "未知" WHERE city IS ;update orderitems set price_new=IF(price_new,5.74);
查询并填充空值列
select AVG(price_new) FROM orderitems;select IF(price_new,5.74) AS bus_ifFROM orderitems;
3 计算列
更新表添加计算列
ALTER TABLE orderitems ADD price_new DECIMAL(8,2) NOT ;update orderitems set price_new= item_price*count;
查询计算列
select item_price*count as sales FROM orderitems;
4 排序
多列排序
select * FROM orderitemsORDER BY price_new DESC,quantity;
查询排名前几的记录
select * FROM orderitemsORDER BY price_new DESC Limit 5;
查询第10大的值
select DISTINCT price_newFROM orderitemsORDER BY price_new DESC LIMIT 9,1;
排名
数值相同的排名相同且排名连续
select prod_price,(select COUNT(DISTINCT prod_price)FROM productsWHERE prod_price>=a.prod_price) AS rankFROM products AS aORDER BY rank ;
5 字符串处理
字符串替换
update data1 SET city=replace(city,'SH','shanghai');select city FROM data1;
按位置字符串截取
字符串截取可用于数据分列
MySQL 字符串截取函数:left, right, substring, substring_index
select left('example.com', 3);
从字符串的第 4 个字符位置开始取,直到结束
select substring('example.com', 4);
从字符串的第 4 个字符位置开始取,只取 2 个字符
select substring('example.com', 4, 2);
按关键字截取字符串
取第一个分隔符之前的所有字符,结果是www
select substring_index('www.google.com','.',1);
取倒数第二个分隔符之后的所有字符,结果是google.com;
select substring_index('www.google.com','.',-2);
6 筛选
通过操作符实现高级筛选
使用 AND OR IN NOT 等操作符实现高级筛选过滤
select prod_name,prod_price FROM ProductsWHERE vend_id IN('DLL01','BRS01');select prod_name FROM Products WHERE NOT vend_id='DLL01';
通配符筛选
常用通配符有% _ ^
select * from customers WHERE country LIKE "CH%";
7 表联结
SQL表连接可以实现类似于Excel中的Vlookup函数的功能
select vend_id,prod_name,prod_priceFROM Vendors INNER JOIN ProductsON Vendors.vend_id=Products.vend_id;select prod_name,vend_name,prod_price,quantityFROM OderItems,Products,VendorsWHERE Products.vend_id=Vendors.vend_idAND OrderItems.prod_id=Products.prod_idAND order_num=20007;
自联结 在一条select语句中多次使用相同的表
select c1.cust_od,c1.cust_name,c1.cust_contactFROM Customers as c1,Customers as c2WHERE c1.cust_name=c2.cust_nameAND c2.cust_contact='Jim Jones';
8 数据透视
数据分组可以实现Excel中数据透视表的功能
数据分组
group by 用于数据分组 having 用于分组后数据的过滤
select order_num,COUNT(*) as itemsFROM OrderItemsGROUP BY order_num HAVING COUNT(*)>=3;
交叉表
通过CASE WHEN函数实现
select data1.city,CASE WHEN colour = "A" THEN price END AS A,CASE WHEN colour = "B" THEN price END AS B,CASE WHEN colour = "C" THEN price END AS C,CASE WHEN colour = "F" THEN price END AS FFROM data1
注:以上代码在MySQL数据库中执行
本文内容由互联网用户自发贡献,该文观点仅代表作者本人。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 203304862@qq.com
本文链接:https://jinnalai.com/jiaodian/25764.html