美文网首页
MySQL 11-12章实操

MySQL 11-12章实操

作者: 可乐要坚持学习 | 来源:发表于2019-02-15 00:18 被阅读0次

第十一章 使用数据处理函数

文本处理函数[常用的文本处理函数说明表]

select vend_name,Upper(vend_name) as VEND_NAME from vendors order by vend_name;
select vend_name,Lower(vend_name) as VEND_NAME from vendors order by vend_name;
select vend_name,length(vend_name) as vend_name_changdu from vendors order by vend_name;
select vend_name,left(vend_name,3) as vend_name_jianxie from vendors order by vend_name;

select cust_name,cust_contact from customers where cust_contact='Y.Lie';   #源数据中存在的是'Y.Lee'而不是'Y.Lie',左侧方式无结果返回#
select cust_name,cust_contact from customers where soundex(cust_contact)=soundex('Y.Lie');    #使用soundex()函数[能对串进行发音比较]即可将结果查找成功# 

常用的文本处理函数中locate()和substring()函数暂时没弄明白如何使用

常用文本函数说明表1
常用文本函数说明表2

日期和时间处理函数[常用的日期和时间处理函数说明表]

select order_num,order_date,cust_id from orders where date(order_date)='2005-09-30';
select order_num,order_date,cust_id from orders where date(order_date) between '2005-09-01' and '2005-09-30';
select order_num,order_date,cust_id from orders where year(order_date)=2005 and month(order_date)=9;
select order_num,order_date,cust_id,adddate(order_date,1) from orders;  
select order_num,order_date,cust_id,addtime(order_date,'1 1:0:0') from orders;
select curdate();
select curtime();
select order_num,order_date,cust_id,date_format(order_date,'%Y-%m-%d') from orders; 

select order_num,order_date,cust_id,dayofweek(order_date) from orders;   #返回的是1-7代表星期几#
select order_num,order_date,cust_id,dayname(order_date) from orders;    #返回的是英文版的星期#
常用日期函数说明表

数值处理函数[常用数值处理函数说明表]#一般用的较少


常用数值函数说明表

第十二章 汇总数据

聚集函数[SQL聚集函数说明表]

select avg(prod_price) as avg_price from products;  #对整列求平均#
select avg(prod_price) as avg_price from products where vend_id=1003;  #对特定列和行求平均#

select count(*) as num_cust from customers;
select count(cust_email) as num_custemail from customers;
#如果指定列明,则指定列的值为空的行被count()忽略,但如果count()函数中使用的是*则不忽略空值行# 

select max(prod_price) as max_price,min(prod_price) as min_price from products;
select order_num,sum(quantity)as sum_quantity from orderitems  where order_num=20005;
select order_num,sum(quantity)as sum_quantity from orderitems  group by order_num order by sum_quantity; 

select order_num,sum(quantity*item_price)as total_price from orderitems  where order_num=20005;
select order_num,sum(quantity*item_price)as total_price from orderitems  group by order_num order by total_price; 
SQL聚集函数说明表

聚合不同值[使用distinct]

SELECT AVG(DISTINCT prod_price) AS avg_price FROM products WHERE vend_id = 1003;
SELECT vend_id, AVG(DISTINCT prod_price) AS avg_price FROM products GROUP BY vend_id;

组合聚集函数

SELECT 
    COUNT(*) AS num_prod,
    AVG(prod_price) AS price_avg,
    MAX(prod_price) AS price_max,
    MIN(prod_price) AS price_min
FROM products;

相关文章

  • MySQL 11-12章实操

    第十一章 使用数据处理函数 文本处理函数[常用的文本处理函数说明表] 常用的文本处理函数中locate()和sub...

  • MySQL 13-16章实操

    第十三章 分组数据 创建分组 过滤分组 分组和排序 第十四章 使用子查询 作为计算字段使用子查询 第十五章 联结表...

  • MYSQL实操日记

    1、连接、登录MySQL 搭建好环境,启动MYSQL服务:net start mysql 然后登录MySQL数据库...

  • mysql复习-实操

    简单信息查询 查看MYSQL版本: select version(); 查看当前用户: select user()...

  • MySQL explain命令实操

    explain作用 explain命令是用来查看一个sql语句的执行计划,可以看出这个sql语句是否使用到索引,是...

  • MySQL 增删改实操

    本文标识 :MQ0004 本文编辑:长安月下赏美人儿 编程工具 :MySQL 阅读时长 :3分钟 数据分析新人小美...

  • Mybatis-2-2.实操总结

    通过上一部分的实操知道了Mybatis的用法。那么这章总结一下上一个实操项目。 实操项目全部源代码:https:/...

  • MySQL主从复制(实操)

    一主一从模式 配置文件路径 /etc/my.cnf 主服务器( /etc/my.cnf ) 基础配置skip_na...

  • 实操Redission

    实操Redission 分布式对象(一)实操Redission 分布式Map集合(二)实操Redission 分布...

  • 生信星球学习记录Day2-HEYi😛

    Linux 基本命令 实操一 实操二 实操三3 实操四 练习题 如何输出长格式列表,以及显示文件大小ls -l ...

网友评论

      本文标题:MySQL 11-12章实操

      本文链接:https://www.haomeiwen.com/subject/mgeueqtx.html