1. 创建计算字段 拼接字段
select concat(vend_name, '(', vend_country, ')') as name from vendors order by vend_name;
删除数据右侧或左侧多余的空格
select concat(RTrim(vend_name), '(', RTrim(vend_country), ')') as name from vendors order by vend_name;
2. 执行算数计算
select prod_id, quantity, item_price,
-> quantity*item_price as expanded_price from orderitems where order_num = 20005;
+---------+----------+------------+----------------+
| prod_id | quantity | item_price | expanded_price |
+---------+----------+------------+----------------+
| ANV01 | 10 | 5.99 | 59.90 |
| ANV02 | 3 | 9.99 | 29.97 |
| TNT2 | 5 | 10 | 50.00 |
| FB | 1 | 10 | 10.00 |
+---------+----------+------------+----------------+
select now(); select trim('ada');
3. 文本处理函数
select vend_name, upper(vend_name) as vend_name_upcase from vendors order by vend_name;
+----------------+------------------+
| vend_name | vend_name_upcase |
+----------------+------------------+
| ACME | ACME |
| Anvils R Us | ANVILS R US |
| Furball Inc. | FURBALL INC. |
| Jet Set | JET SET |
| Jouets Et Ours | JOUETS ET OURS |
| LT Supplies | LT SUPPLIES |
+----------------+------------------+
Soundex 的使用,发音的匹配
select cust_name, cust_contact from customers where soundex(cust_contact) = soundex('Y Lie');
+-------------+--------------+
| cust_name | cust_contact |
+-------------+--------------+
| Coyote Inc. | Y Lee |
+-------------+--------------+
4. 日期和时间处理函数
AddDate() 增加一个日期
AddTime() 增加一个时间
CurDate() 返回当前日期
CurTime() 返回当前时间
Date() 返回日期时间的日期部分
DateFiff() 计算两个日期之差
Date_Add() 高度灵活的日期运算函数
Date_Format() 返回一个格式化的日期或时间串
Day() 返回一个日期的天数部分
DayOfWeek() 对于一个日期,返回对应的星期
Hour() 返回时间的小时部分
Minute() 返回一个时间的分钟部分
Month() 返回一个日期的月份部分
Now() 返回当前日期和时间
Second() 返回一个时间的秒部分
Time() 返回一个日期时间的时间部分
Year() 返回一个日期的年份部分
select cust_id, order_num from orders where order_date = '2005-09-01';
Datetime 类型只匹配日期 2005-09-01 11:30:05
select cust_id, order_num from orders where Date(order_date) = '2005-09-01';
检索2005年9月份所有订单
select cust_id, order_num from orders
-> where Date(order_date) between '2005-09-01' and '2005-09-30';
select cust_id, order_num from orders
-> where Year(order_date) = 2005 and Month(order_date) = 9;
select cust_id, order_num from orders
-> where Date(order_date) >= '2005-09-01' and Date(order_date) <= '2005-09-30';
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
| 10003 | 20006 |
| 10004 | 20007 |
+---------+-----------+
5. 聚集函数 运行在行组上,计算和返回单个值的函数
AVG() 返回某列的平均值
COUNT() 返回某列的行数
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列之和
AVG() 忽略NULL 值的行
select AVG(prod_price) as avg_price from products;
+-----------+
| avg_price |
+-----------+
| 16.133571 |
+-----------+
COUNT()
COUNT(*) 对表中行的数目进行计数,不管表列中包含的是空(NULL),还是非空值。
COUNT(column)对表中特定劣种具有值的进行计数,忽略NULL 值。
select count(cust_email) as num_cust from customers;
+----------+
| num_cust |
+----------+
| 3 |
+----------+
MAX() MIN()
select max(prod_price) as max_price, prod_name from products;
+-----------+--------------+
| max_price | prod_name |
+-----------+--------------+
| 55 | .5 ton anvil |
+-----------+--------------+
SUM()
select sum(quantity) as items_ordered from orderitems where order_num = 20005;
+---------------+
| items_ordered |
+---------------+
| 19 |
+---------------+
SUM 也可以用来合计计算值
select sum(quantity*item_price) as total_price from orderitems where order_num = 20005;
+-------------+
| total_price |
+-------------+
| 149.87 |
+-------------+
聚集不同值 DISTINCT
select AVG(distinct prod_price) as avg_price from products where vend_id = 1003;
+-----------+
| avg_price |
+-----------+
| 15.998 |
+-----------+
组合聚集函数
select count(*) as num_items,
-> min(prod_price) as price_min,
-> max(prod_price) as price_max,
-> avg(prod_price) as price_avg
-> from products;
+-----------+-----------+-----------+-----------+
| num_items | price_min | price_max | price_avg |
+-----------+-----------+-----------+-----------+
| 14 | 2.5 | 55 | 16.133571 |
+-----------+-----------+-----------+-----------+
6. 分组数据
select vend_id, count(*) as num_prods from products group by vend_id;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
+---------+-----------+
使用 with rollup 可以得到每个分组以及每个分组汇总级别的值。
select vend_id, count(*) as num_prods from products group by vend_id with rollup;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1002 | 2 |
| 1003 | 7 |
| 1005 | 2 |
| NULL | 14 |
+---------+-----------+
过滤分组
select vend_id, count(*) as num_prods from products group by vend_id having count(*) >= 3;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1001 | 3 |
| 1003 | 7 |
+---------+-----------+
WHERE 不起作用,因为过滤式基于分组聚集值而不是特定行值的。
WHERE 在分组前过滤,HAVING 在分组后过滤。
select vend_id, count(*) as num_prods from products where prod_price >= 10 group by
-> vend_id having count(*) >= 2;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| 1003 | 4 |
| 1005 | 2 |
+---------+-----------+
分组和排序
检索总计订单价格大于等于50的订单的订单号和总计订单价格
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by
-> order_num having sum(quantity*item_price) >= 50;
+-----------+------------+
| order_num | ordertotal |
+-----------+------------+
| 20005 | 149.87 |
| 20006 | 55.00 |
| 20007 | 1000.00 |
| 20008 | 125.00 |
+-----------+------------+
等价:
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by
-> order_num having ordertotal >= 50;
7. 子查询
列出订购物品TNT2的所有客户
select cust_name, cust_contact
-> from customers where cust_id in
-> (select cust_id from orders where order_num in (select order_num from orderitems where prod_id = 'TNT2'));
+----------------+--------------+
| cust_name | cust_contact |
+----------------+--------------+
| Coyote Inc. | Y Lee |
| Yosemite Place | Y Sam |
+----------------+--------------+
显示customers 表中每个客户的订单总数。
select cust_name, cust_state, (select count(*) from orders where orders.cust_id = customers.cust_id)
-> as orders from customers order by cust_name;
+----------------+------------+--------+
| cust_name | cust_state | orders |
+----------------+------------+--------+
| Coyote Inc. | MI | 2 |
| E Fudd | IL | 1 |
| Mouse House | OH | 0 |
| Wascals | IN | 1 |
| Yosemite Place | AZ | 1 |
+----------------+------------+--------+
8. 联结表
不要忘记WHERE 字句,所有的联结都有WHERE 字句。
内联结(等值联结)
select vend_name, prod_name, prod_price from vendors, products where
-> vendors.vend_id = products.vend_id
-> order by vend_name, prod_name;
等价
select vend_name, prod_name, prod_price from
-> vendors inner join products on vendors.vend_id = products.vend_id;
联结多个表
select cust_name, cust_contact from customers, orders, orderitems
-> where customers.cust_id = orders.cust_id
-> and orderitems.order_num = orders.order_num
-> and prod_id = 'TNT2';
+----------------+--------------+
| cust_name | cust_contact |
+----------------+--------------+
| Coyote Inc. | Y Lee |
| Yosemite Place | Y Sam |
+----------------+--------------+
使用表别名
select Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') as vend_title
-> from vendors order by vend_name;
+-------------------------+
| vend_title |
+-------------------------+
| ACME (USA) |
| Anvils R Us (USA) |
| Furball Inc. (USA) |
| Jet Set (England) |
| Jouets Et Ours (France) |
| LT Supplies (USA) |
+-------------------------+
自联结(通常作为外部语句用来替代从相同表中检索数据时使用的子查询语句)
查找物品ID 为DINTR的其他物品
select prod_id, prod_name from products
-> where vend_id = (select vend_id from products where prod_id = 'DTNTR');
等价
select p1.prod_id, p1.prod_name from
-> products as p1, products as p2 where
-> p1.vend_id = p2.vend_id
-> and p2.prod_id = 'DTNTR';
外部联结
分为左外部联结和右外部联结
select customers.cust_id, orders.order_num from
-> customers left outer join orders on
-> customers.cust_id = orders.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
| 10001 | 20009 |
| 10002 | NULL |
| 10003 | 20006 |
| 10004 | 20007 |
| 10005 | 20008 |
+---------+-----------+
使用带聚集函数的联结
select customers.cust_name, customers.cust_id, COUNT(orders.order_num) as num_ord
-> from customers inner join orders
-> on customers.cust_id = orders.cust_id group by customers.cust_id;
+----------------+---------+---------+
| cust_name | cust_id | num_ord |
+----------------+---------+---------+
| Coyote Inc. | 10001 | 2 |
| Wascals | 10003 | 1 |
| Yosemite Place | 10004 | 1 |
| E Fudd | 10005 | 1 |
+----------------+---------+---------+
左外联结
select customers.cust_name, customers.cust_id, COUNT(orders.order_num) as num_ord
-> from customers left outer join orders
-> on customers.cust_id = orders.cust_id group by customers.cust_id;
+----------------+---------+---------+
| cust_name | cust_id | num_ord |
+----------------+---------+---------+
| Coyote Inc. | 10001 | 2 |
| Mouse House | 10002 | 0 |
| Wascals | 10003 | 1 |
| Yosemite Place | 10004 | 1 |
| E Fudd | 10005 | 1 |
+----------------+---------+---------+
分享到:
相关推荐
数据库 MySQL 学习笔记高级篇.md
这份“MySQL学习笔记”将引导我们深入理解其核心概念和实用技能。 一、MySQL简介 MySQL是一个开源、免费的数据库系统,由瑞典的MySQL AB公司开发,后被Oracle公司收购。它的设计目标是速度、可移植性和简洁性,支持...
MySQL学习笔记.chmMySQL学习笔记.chmMySQL学习笔记.chmMySQL学习笔记.chm
MySQL是世界上最受欢迎的关系型数据库管理系统之一,尤其在Web应用...学习MySQL不仅涉及理论知识,还需要大量的实践操作来熟悉其语法和功能。掌握MySQL能让你在数据管理、Web开发和系统集成等领域具备更强的竞争力。
这份"mysql学习笔记.rar"包含的资源旨在帮助初学者和有经验的开发者深入理解和掌握MySQL的相关知识。 首先,"mysql学习笔记.doc"很可能是核心的学习资料,它可能包含了MySQL的基础概念、安装与配置、SQL语言基础、...
mysql入门学习笔记整理,如何创建数据库、查看数据库,删除数据库,创建表和对表字段类型操作、约束、mysql数据类型整理
MySQL 是一款广泛使用的开源关系型数据库管理系统,其学习笔记涵盖了多个关键知识点。以下是对这些知识点的详细解释: 1. **MySQL 体系架构** - **网络连接层**:处理客户端的连接请求,包括连接管理、认证和安全...
### MySQL核心技术学习笔记 #### 一、为什么要学习数据库 学习数据库的重要性主要体现在以下几个方面: 1. **持久化数据到本地**:数据库能够将应用程序产生的数据持久化存储在磁盘上,即使系统重启也不会丢失...
### MySQL学习笔记1:深入理解内连接与外连接 #### 一、引言 在数据库管理与查询过程中,连接操作是十分常见且重要的一个环节。MySQL作为一种广泛使用的开源关系型数据库管理系统,在处理复杂的多表查询时,提供了...
非常详细的mysql学习笔记,3积分绝对值。内容分为基础架构、日志系统、事务隔离、索引、锁、sql偶尔变慢的问题、数据空洞、排序、慢sql解析九大模块。
### MySQL学习笔记知识点详解 #### 一、数据库概述 数据库是指按照特定的数据结构来组织、存储和管理数据的集合体。随着信息技术的进步和社会需求的增长,数据库已经从最初的简单存储发展成为用户所需的各种复杂...
MySQL学习笔记.pdf MySQL学习笔记主要涵盖了 MySQL 服务器的基本操作、数据库管理、数据表的创建和管理、数据的crud 操作、约束的添加和删除等内容。 一、登录和退出 MySQL 服务器 在 MySQL 学习笔记中,首先...
mysql学习笔记
MySQL是世界上最受欢迎的关系型数据库管理...以上只是MySQL学习笔记的一部分内容,实际的学习过程中,你还会接触到触发器、分区、复制、集群等更高级的主题。不断实践和深入研究,才能真正掌握这个强大的数据库系统。
从给定的文件信息来看,本文档主要围绕“MySQL学习笔记—JSP操作MySQL”的主题展开,详细介绍了如何在JSP环境下操作MySQL数据库的过程。以下是对该文档中的关键知识点进行的深入解析: ### 一、MySQL与JSP的集成 ...
MySQL学习笔记(含基础、运维、进阶三部分) MySQL学习笔记(含基础、运维、进阶三部分) MySQL学习笔记(含基础、运维、进阶三部分) 包含了我学习 MySQL 过程中的笔记和资源,从入门到进阶的内容都有涉及。通过...
在MySQL学习过程中,了解和掌握基本的命令是非常重要的。以下是一些关键知识点的详细解释: 1. **显示数据库**: 使用`SHOW DATABASES;`命令可以列出当前MySQL服务器上存在的所有数据库。 2. **创建数据库**: `...
这份“mysql学习笔记”涵盖了MySQL的基础知识,包括但不限于数据库的概念、SQL语言的使用、以及更深入的多表操作。 在“day08入门笔记”中,你可能会学到以下内容: 1. **数据库基本概念**:了解什么是数据库,...