【linux】循序渐进学运维-MySQL-SQL语句进阶

1. 数据类型

mysql中定义数据字段的类型对数据库的优化是非常重要的,mysql中支持的类型大致分为:数值,日期/时间,字符串(字符)类型

1) 数值类型如图:

在这里插入图片描述

2) 日期和时间类型

在这里插入图片描述

3)字符串类型

在这里插入图片描述

4) 整型:

在这里插入图片描述

5) 浮点型:

在这里插入图片描述

6) 字符型:

在这里插入图片描述

2. 常用的SQL命令:

1) select命令
使用select命令查看mysql数据库系统信息:
-- 打印当前的日期和时间
select now();
-- 打印当前的日期
select curdate();
-- 打印当前的时间
select curtime();

-- 打印当前数据库
select database();

-- 打印MySQL版本
select version();

-- 打印当前用户
select user();

--查看系统信息
show variables;     #显示变量
show global variables;  #显示全局变量
show global variables like '%version%';
show variables like '%storage_engine%'; 		#默认的存储引擎
like和”_”模糊搜索还可用户where字句。
MySQL提供两个通配符,用于与LIKE运算符一起使用,它们分别是:百分比符号 %和下划线 _。
百分比(%)表示通配符允许匹配任何字符串的零个或多个字符。
下划线(_)表示通配符允许匹配任何单个字符。

2) 下划线表示通配任意单个字符
mysql> 
mysql> create database zmedu;
Query OK, 1 row affected (0.00 sec)

mysql> use zmedu
Database changed
mysql> create table students(id int(11),stname char(20));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into students values(1,'liuyong'),(1,'zhangru'),(3,'laowang'),(4,'tongtong');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from students where stname like '%_';
+------+----------+
| id   | stname   |
+------+----------+
|    1 | liuyong  |
|    1 | zhangru  |
|    3 | laowang  |
|    4 | tongtong |
+------+----------+
4 rows in set (0.00 sec)

mysql>
3) 有like,就有not like, 意思相反

not like 实例

mysql> select * from students where stname not like '%g';
+------+---------+
| id   | stname  |
+------+---------+
|    1 | zhangru |
+------+---------+
1 row in set (0.00 sec)

4) 查看系统运行状态
查看系统运行状态信息:
mysql> show status; 		#查看运行状态
mysql> show global status like 'Thread%'; 		#显示全局状态

逻辑运算
and or not
and 且
or 或
not 非

扫描二维码关注公众号,回复: 9184616 查看本文章

实例:

导入book.sql
mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;
+--------------------------------------+--------------------------+-------+
| bName                                | publishing               | price |
+--------------------------------------+--------------------------+-------+
| Illustrator 10完全手册           | 科学出版社          |    50 |
| FreeHand 10基础教程              | 北京希望电子出版 |    50 |
| 网站设计全程教程             | 科学出版社          |    50 |
| ASP数据库系统开发实例导航 | 人民邮电出版社    |    60 |
| Delphi 5程序设计与控件参考  | 电子工业出版社    |    60 |
| ASP数据库系统开发实例导航 | 人民邮电出版社    |    60 |
+--------------------------------------+--------------------------+-------+
6 rows in set (0.00 sec)

5). 算术运算符

= 等于
<> 不等于 !=

大于
< 小于
= 大于等于
<= 小于等于

in 运算符
IN 运算符用于 WHERE 表达式中,以列表项的形式支持多个选择,语法如下:
WHERE column IN (value1,value2,…)
WHERE column NOT IN (value1,value2,…)

not in 与in相反
当 IN 前面加上 NOT 运算符时,表示与 IN 相反的意思,即不在这些列表项内选择

mysql> select bName,price from books where price >60;
mysql> select bName,price from books where price =60;
+--------------------------------------+-------+
| bName                                | price |
+--------------------------------------+-------+
| ASP数据库系统开发实例导航 |    60 |
| Delphi 5程序设计与控件参考  |    60 |
| ASP数据库系统开发实例导航 |    60 |
+--------------------------------------+-------+
3 rows in set (0.00 sec)
mysql> select bName,price from books where price in (50,60,70);
+--------------------------------------+-------+
| bName                                | price |
+--------------------------------------+-------+
| Illustrator 10完全手册           |    50 |
| FreeHand 10基础教程              |    50 |
| 网站设计全程教程             |    50 |
| ASP数据库系统开发实例导航 |    60 |
| Delphi 5程序设计与控件参考  |    60 |
| ASP数据库系统开发实例导航 |    60 |
+--------------------------------------+-------+
6 rows in set (0.00 sec)

6). 排序

升序:order by “排序的字段” asc 默认
降序:oredr by “排序的字段” desc

mysql> select bName,price from books where price in (50,60,70) order by price asc
    -> ;
+--------------------------------------+-------+
| bName                                | price |
+--------------------------------------+-------+
| Illustrator 10完全手册           |    50 |
| FreeHand 10基础教程              |    50 |
| 网站设计全程教程             |    50 |
| ASP数据库系统开发实例导航 |    60 |
| Delphi 5程序设计与控件参考  |    60 |
| ASP数据库系统开发实例导航 |    60 |
+--------------------------------------+-------+
6 rows in set (0.00 sec)

7). 范围运算

[not]between …and…
Between and 可以使用大于小于的方式来代替,并且使用大于小于意义表述更明确
实例:
查找价格不在30到60之间的书名和价格

mysql> select bName,price from books where price not between 30 and 60 order by price desc;
+---------------------------------------------------------+-------+
| bName                                                   | price |
+---------------------------------------------------------+-------+
| Javascript与Jscript从入门到精通                  |  7500 |
| XML 完全探索                                        |   104 |
| ASP 3初级教程                                       |   104 |
| SQL Server 7.0数据库系统管理与应用开发      |    95 |
| SQL Server 2000 从入门到精通                      |    93 |
| 3D Studio Max 3综合使用                             |    91 |
| lllustrator 9宝典                                     |    83 |
| 3D MAX R3动画制作与培训教程                    |    73 |
| HTML设计实务                                        |    72 |
| Frontpage 2000& ASP 网页设计技巧与网站维护 |    71 |
| Auto CAD R14 中文版实用教程                      |    64 |
| 深入Flash 5教程                                     |    64 |
| 精通Javascript                                        |    63 |
| 3DS MAX 4横空出世                                   |    63 |
| Auto CAD 2002 中文版实用教程                     |    63 |
| 活学活用Delphi5                                     |    62 |
+---------------------------------------------------------+-------+
16 rows in set (0.00 sec)


8). 模糊匹配

字段名 [not]like ‘通配符’ ----》% 任意多个字符

mysql> select bName from books where bName like '%程序%';
+-------------------------------------+
| bName                               |
+-------------------------------------+
| 网络程序与设计-asp         |
| Delphi 5程序设计与控件参考 |
+-------------------------------------+
2 rows in set (0.00 sec)

9). mysql子查询

概念:在select 的where条件中又出现了select,查询中嵌套着查询

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='网络技术');
+----------------------+---------+
| bName                | bTypeId |
+----------------------+---------+
| Internet操作技术 | 7       |
+----------------------+---------+
1 row in set (0.00 sec)

10). limit限定显示条目

SELECT * FROM table LIMIT [offset,] rows
偏移量 行数
  LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1)。

比如select * from table limit m,n语句
表示其中m是指记录开始的index,从0开始,表示第一条记录
n是指从第m+1条开始,取n条。

查出category表中第2条到第6行的记录。
首先2到6行有2,3,4,5,6总共有5个数字,从2开始,偏移量为1

mysql> select * from category limit 1,5;
+---------+--------------+
| bTypeId | bTypeName    |
+---------+--------------+
|       2 | 网站       |
|       3 | 3D动画     |
|       4 | linux学习  |
|       5 | Delphi学习 |
|       6 | 黑客       |
+---------+--------------+
5 rows in set (0.00 sec)
发布了256 篇原创文章 · 获赞 56 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/xinshuzhan/article/details/102827635