MySQL基础部分总结

MySQL

1、选择数据库

  1. use dbname
  2. show databases;

2、数据表

  1. show tables
  2. mysql> show columns from customers;
  3. mysql> desc customers;

3、show 语句

  1. show status
  2. show create databases
  3. show create table
  4. show grants

4、select 检索

4.1.1版本后不再区分大小写,但是为了容易阅读和调试,建议还是使用。

mysql> select cust_name from customers;
mysql> select cust_name cust_status from customers;
mysql> select distinct vend_id from products;
mysql> select prod_name from products limit 5;
mysql> select prod_name from products limit 5,5;
//分页问题
从行0开始计数,limit5,5是从第5行开始(不包括第五行),取5行,结果是:6:10行
因为这个很容易引起误解,所以MySQL5开始支持另一个语法:limit 4 offset 3,意思是从行3开始取4行,等同于limit 3,4

4-1、排序数据

//单个字段排序
mysql> select prod_name from products order by  prod_name;
//多个字段排序,如果第一个字段全部唯一则第二个字段就不会生效
mysql> select prod_id,prod_price,prod_name from products order by prod_price ,prod_name;

4-2、指定排序方向

  • desc 降序
  • asc 升序-默认

注意顺序,from>ordrr by >limit

mysql> select prod_id,prod_price,prod_name from products order by prod_price desc;
mysql> select prod_id,prod_price,prod_name from products order by prod_price asc;
mysql> select prod_price from products order by prod_price desc limit 1;

5、where 条件

相关操作符:

  1. = 等于
  2. <> 不等于
  3. != 不等于
  4. < 小于
  5. > 大于
  6. >= 大于或者等于
  7. <= 小于或等于
  8. between 两者之间 and
and 的优先级大于or,需要处理or,则需要括号
mysql> select prod_price,prod_name from  products where prod_price = 2.50;
mysql> select prod_price,prod_name from  products where prod_price  between 5 and 10;
//  IS NULL
mysql> select cust_id from customers where cust_email is null;
重点:空值检查
空值既是:NULL
MySQL中判断是否是空值的子句是: IS NULL

example:

 mysql> select cust_id FROM customers  where cust_email IS NULL;    
+---------+
| cust_id |
+---------+
|   10002 |
|   10005 |
+---------+

6、where 数据过滤

(logical operator)逻辑操作符:and - or

mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 and prod_price<= 10;
mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 or vend_id = 1002;

运算符优先级问题:
下列SQL中实际先运行 vend_id = 1002 and prod_price >= 10;,再运行vend_id = 1003.因为and的优先级大于or,如果要按理想执行,加括号!

mysql> select prod_id,prod_price,prod_name from products where vend_id = 1003 or vend_id = 1002 and prod_price >= 10;
mysql> select prod_id,prod_price,prod_name from products where (vend_id = 1003 or vend_id = 1002 )and prod_price >= 10;

6-1、 in操作符 (not in)

mysql> select prod_id,prod_price,prod_name from products where vend_id in (1002,1003) order by prod_name;

6-2、 or操作符

mysql> select prod_id,prod_price,prod_name from products where vend_id not in (1002,1003) order by prod_name;

7、用通配符过滤

like 和 _ 的区别是后者只能匹配一个字符

7-1、like

**注意NULL 虽然似乎 % 通配符可以匹配任何东西,但有一个例
外,即 NULL 。即使是 WHERE prod_name LIKE '%' 也不能匹配
用值 NULL 作为产品名的行。**

mysql> select prod_id,prod_price,prod_name from products where prod_name  LIKE 'jet%';mysql> select prod_id,prod_price,prod_name from products where prod_name  LIKE '%anv%';

7-2、_

mysql> select prod_id,prod_price,prod_name from products where prod_name  LIKE '_ ton anvil';

8、正则表达式

like是匹配全部,REGEXP可以匹配全部和部分

mysql> select prod_name  from products where prod_name ='JetPack 1000';
+--------------+
| prod_name    |
+--------------+
| JetPack 1000 |
+--------------+
1 row in set (0.00 sec)mysql> select prod_name from products where prod_name  REGEXP '1000';
+--------------+
| prod_name    |
+--------------+
| JetPack 1000 |
+--------------+
1 row in set (0.00 sec)

默认不区分大小写,需要区分大小写binary

mysql> select prod_name from products where prod_name  REGEXP 'jetpack .000';
mysql> select prod_name from products where prod_name  REGEXP binary 'JetPack .000';

10、计算字段

  1. concat 合并 讲两个字段合并成一个新的字段
mysql> select concat (vend_name , 'C',vend_country,')') from vendors order by vend_name;
+-------------------------------------------+
| concat (vend_name , 'C',vend_country,')') |
+-------------------------------------------+
| ACMECUSA)                                 |
| Anvils R UsCUSA)                          |
| Furball Inc.CUSA)                         |
| Jet SetCEngland)                          |
| Jouets Et OursCFrance)                    |
| LT SuppliesCUSA)                          |
+-------------------------------------------+
6 rows in set (0.00 sec)
  1. rtrim (ltrim ,trim) 去掉空格
mysql> select concat (rtrim(vend_name) , 'C',vend_country,')') from vendors order by vend_name;
  1. as 别名
mysql> select concat (rtrim(vend_name) , '(',rtrim(vend_country),')') as vend_title   from vendors order by vend__name;
  1. 计算

+、-、* 、\

mysql> select quantity*item_price as expand_price from orderitems where order_num =20005;

11、函数

  1. trim、ltrim、rtrim 去掉空值
  2. Upper 转为大写

mysql> select vend_name,upper(vend_name) as ven_name_upcase from vendors order by vend_name;

11-2 时间函数

  1. AddDate() 增加一个日期(天、周等)
  2. AddTime() 增加一个时间(时、分等)
  3. CurDate() 返回当前日期
  4. CurTime() 返回当前时间
  5. ==Date() 返回日期时间的日期部分==
  6. DateDiff() 计算两个日期之差
  7. Date_Add() 高度灵活的日期运算函数
  8. Date_Format() 返回一个格式化的日期或时间串
  9. Day() 返回一个日期的天数部分
  10. DayOfWeek() 对于一个日期,返回对应的星期几
  11. Hour() 返回一个时间的小时部分
  12. Minute() 返回一个时间的分钟部分
  13. Month() 返回一个日期的月份部分
  14. Now() 返回当前日期和时间
  15. Second() 返回一个时间的秒部分
  16. Time() 返回一个日期时间的时间部分
  17. Year() 返回一个日期的年份部分

取9月某一天的数据

mysql> select cust_id,order_num from orders where Date(order_date) = '2005-09-01'; 
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
+---------+-----------+
1 row in set (0.00 sec)

取9月整个月的订单

mysql> select cust_id,order_num from orders where Date(order_date)  between '2005-09-01' and '2005-09-30'; 
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
|   10003 |     20006 |
|   10004 |     20007 |
+---------+-----------+
3 rows in set (0.00 sec)mysql> select cust_id,order_num from orders where Year(order_date) and month(order_date) = 9;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
|   10003 |     20006 |
|   10004 |     20007 |
+---------+-----------+
3 rows in set (0.00 sec)

11-4 数值处理函数

  1. Abs() 返回一个数的绝对值
  2. Cos() 返回一个角度的余弦
  3. Exp() 返回一个数的指数值
  4. Mod() 返回除操作的余数
  5. Pi() 返回圆周率
  6. Rand() 返回一个随机数
  7. Sin() 返回一个角度的正弦
  8. Sqrt() 返回一个数的平方根
  9. Tan() 返回一个角度的正切

11-5 聚集函数

  1. AVG() 返回某列的平均值
  2. COUNT() 返回某列的行数
  3. MAX() 返回某列的最大值
  4. MIN() 返回某列的最小值
  5. SUM() 返回某列值之和
  6. DISTINCT
mysql> select avg(prod_price) as avg_price from products;

分组数据

GROUP BY子句和HAVING子句

mysql> 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 |
+---------+-----------+
4 rows in set (0.00 sec)mysql> 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 |
+---------+-----------+
5 rows in set (0.00 sec)
having
唯一的差别是 WHERE过滤行,而HAVING过滤分组。WHERE在数据 分组前进行过滤,HAVING在数据分组后进行过滤
mysql> select vend_id,count(*) as num_prods from products group by vend_id having count(*)>=2;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
|    1001 |         3 |
|    1002 |         2 |
|    1003 |         7 |
|    1005 |         2 |
+---------+-----------+
4 rows in set (0.00 sec)mysql> 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 |
+---------+-----------+
2 rows in set (0.00 sec)mysql> select order_num ,sum(quantity*item_price) as ordertotal from orderitems 
-> group by order_num
-> having sum(quantity*item_price) >= 50
-> order by ordertotal;
+-----------+------------+
| order_num | ordertotal |
+-----------+------------+
|     20006 |      55.00 |
|     20008 |     125.00 |
|     20005 |     149.87 |
|     20007 |    1000.00 |
+-----------+------------+
4 rows in set (0.00 sec)
顺序
  • select
  • from
  • where
  • group by
  • having
  • order by
  • limit

12 子查询

mysql>  select cust_id from orders where order_num in  (select order_num from orderitems where prod_id ='TNT2');
+---------+
| cust_id |
+---------+
|   10001 |
|   10004 |
+---------+

15 连接表

笛卡儿积(cartesian product)

如果将两个表同时作为数据源(from后的表名),不加任何的匹配条件,那么产生的结果集就是一个迪卡尔积。
迪卡尔积的结果没有意义,但是迪卡尔积是联合查询、连接查询的基础。

1. 交叉连接 cross join

使用表A中的1条记录去表B中连接所有的记录,就是笛卡尔积

2. 内连接

select 字段列表 from 表A 【inner】 join 表B ,匹配到的成功的记录

3. 外连接 分为左连接和右连接,

左连接保留左边的所有,右边匹配到的部分

4. using关键字

在进行连接时,如果进行连接的两个字段的名子相同,则可以使用using using('cid')
当前笔记出自 《MySQL必知必会》

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

BZOJ2503: 相框

Description P大的基础电路实验课是一个无聊至极的课。每次实验&#xff0c;T君总是提前完成&#xff0c;管理员却不让T君离开&#xff0c;T君只能干坐在那儿无所事事。先说说这个实验课&#xff0c;无非就是把几根导线和某些元器件&#xff08;电阻、电容、电感等&#xff09;…

泰坦尼克号 数据分析_第1部分:泰坦尼克号-数据分析基础

泰坦尼克号 数据分析My goal was to get a better understanding of how to work with tabular data so I challenged myself and started with the Titanic -project. I think this was an excellent way to learn the basics of data analysis with python.我的目标是更好地了…

Imperva开源域目录控制器,简化活动目录集成

Imperva已公开发布域目录控制器&#xff08;Domain Directory Controller&#xff0c;DDC&#xff09;的源代码&#xff0c;这是一个Java库&#xff0c;用于简化常见的Active Directory集成。 与Java的LdapContext不同&#xff0c;这个库构建在Apache Directory LDAP之上&#…

2018.10.24 NOIP模拟 小 C 的序列(链表+数论)

传送门 考虑到a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])是可以分成最多logloglog段且段内的数都是相同的。 那么我们用…

vba数组dim_NDArray — —一个基于Java的N-Dim数组工具包

vba数组dim介绍 (Introduction) Within many development languages, there is a popular paradigm of using N-Dimensional arrays. They allow you to write numerical code that would otherwise require many levels of nested loops in only a few simple operations. Bec…

Nodejs教程08:同时处理GET/POST请求

示例代码请访问我的GitHub&#xff1a; github.com/chencl1986/… 同时处理GET/POST请求 通常在开发过程中&#xff0c;同一台服务器需要接收多种类型的请求&#xff0c;并区分不同接口&#xff0c;向客户端返回数据。 最常用的方式&#xff0c;就是对请求的方法、url进行区分判…

关于position的四个标签

四个标签是static&#xff0c;relative&#xff0c;absolute&#xff0c;fixed。 static 该值是正常流&#xff0c;并且是默认值&#xff0c;因此你很少看到&#xff08;如果存在的话&#xff09;指定该值。 relative&#xff1a;框的位置能够相对于它在正常流中的位置有所偏移…

python算法和数据结构_Python中的数据结构和算法

python算法和数据结构To至 Leonardo da Vinci达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this article is to give you a panorama of data structures and algorithms in Python. This topic is very important for a Data Scientist in order to help …

CSS:元素塌陷问题

2019独角兽企业重金招聘Python工程师标准>>> 描述&#xff1a; 在文档流中&#xff0c;父元素的高度默认是被子元素撑开的&#xff0c;也就是子元素多高&#xff0c;父元素就多高。但是当子元素设置浮动之后&#xff0c;子元素会完全脱离文档流&#xff0c;此时将会…

Celery介绍及常见错误

celery 情景&#xff1a;用户发起request&#xff0c;并等待response返回。在本些views中&#xff0c;可能需要执行一段耗时的程序&#xff0c;那么用户就会等待很长时间&#xff0c;造成不好的用户体验&#xff0c;比如发送邮件、手机验证码等。 使用celery后&#xff0c;情况…

python dash_Dash是Databricks Spark后端的理想基于Python的前端

python dash&#x1f4cc; Learn how to deliver AI for Big Data using Dash & Databricks this recorded webinar with Peter Kim of Plotly and Prasad Kona of Databricks.this通过Plotly的Peter Kim和Databricks的Prasad Kona的网络研讨会了解如何使用Dash&#xff06…

js里的数据类型转换

1、类型转换 转换为字符串 - String(x)- x.toString(x, 10)- x 转换为数字 - Number(x)- parseInt(x, 10) - parseFloat(x) - x - 0- x 转换为boolean - Boolean(x)- !!x 2、falsy值&#xff08;false&#xff09; - 0- NaN- - null- undefined 3、内存图 - object存储的是地址…

Eclipse 插件开发遇到问题心得总结

Eclipse 插件开发遇到问题心得总结 Posted on 2011-07-17 00:51 季枫 阅读(3997) 评论(0) 编辑 收藏1、Eclipse 中插件开发多语言的实现 为了使用 .properties 文件&#xff0c;需要在 META-INF/MANIFEST.MF 文件中定义&#xff1a; Bundle-Localization: plugin 这样就会…

/src/applicationContext.xml

<?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns:context"http://www.springframework.org/schema…

在Python中查找子字符串索引的5种方法

在Python中查找字符串中子字符串索引的5种方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() str.find() str.rfind() str.rfind() str.index() str.index() str.rindex() str.rindex() re.search() re.search() str.find() (str.find()) …

[LeetCode] 3. Longest Substring Without Repeating Characters 题解

问题描述 输入一个字符串&#xff0c;找到其中最长的不重复子串 例1&#xff1a; 输入&#xff1a;"abcabcbb" 输出&#xff1a;3 解释&#xff1a;最长非重复子串为"abc" 复制代码例2&#xff1a; 输入&#xff1a;"bbbbb" 输出&#xff1a;1 解…

WPF中MVVM模式的 Event 处理

WPF的有些UI元素有Command属性可以直接实现绑定&#xff0c;如Button 但是很多Event的触发如何绑定到ViewModel中的Command呢&#xff1f; 答案就是使用EventTrigger可以实现。 继续上一篇对Slider的研究&#xff0c;在View中修改Interaction. <i:Interaction.Triggers>&…

Eclipse 插件开发 向导

阅读目录 最近由于特殊需要&#xff0c;开始学习插件开发。   下面就直接弄一个简单的插件吧!   1 新建一个插件工程   2 创建自己的插件名字&#xff0c;这个名字最好特殊一点&#xff0c;一遍融合到eclipse的时候&#xff0c;不会发生冲突。   3 下一步&#xff0c;进…

线性回归 假设_线性回归的假设

线性回归 假设Linear Regression is the bicycle of regression models. It’s simple yet incredibly useful. It can be used in a variety of domains. It has a nice closed formed solution, which makes model training a super-fast non-iterative process.线性回归是回…

ES6模块与commonJS模块的差异

参考&#xff1a; 前端模块化 ES6 在语言标准的层面上&#xff0c;实现了模块功能&#xff0c;而且实现得相当简单&#xff0c;旨在成为浏览器和服务器通用的模块解决方案。 其模块功能主要由两个命令构成&#xff1a;export和import。export命令用于规定模块的对外接口&#x…