码迷,mamicode.com
首页 > 数据库 > 详细

4、MySQL查询语句01

时间:2017-11-15 23:44:48      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:数据库查询   备份   还原   逻辑运算   

上一章内容介绍了关于MySQL数据类型组成及应用,本章将介绍MySQL中的一些查询语句一些常用的命令和使用实例

一、相关命令语法格式

1.1 添加字段

alter table tb_name add <字段名> 列类型 [not null| null][primary key][uniqe][auto_increment][default value]
alter table tb_name add <字段定义> after <已有字段>

1.2 删除字段

alter table tb_name drop 字段名

1.3 修改字段类型

alter table tb_name modify <字段名> 字段新类型;

1.4 修改字段名和类型

alter table tb_name change <旧字段名称> <新字段定义>

1.5 修改表名

alter table OLD_tb_name rename NEW_tb_name;

1.6 删除表(并避免报错)

drop table [fi (not) exists] tb_name;

1.7 表中行的操作-insert

语法:insert [into] tb_name [(字段列表)] values|value(表达式|null|default,...),(表达式|null|default...)
insert [into] tb_name set 字段名称=值,...
insert与insert ... set的区别是后者可以带有子查询。

1.8 表中行的操作-update

update tb_name set 字段名称=值,...[where 条件];默认省略是更显全部记录的使用需谨慎

1.9 表中行的操作-delete

delete from tb_name[where 条件]
;同上,不加where条件限制则删除全部记录

1.10 表中行的操作-select

select 字段列表 from tb_name [as tb_alias] [where 条件];使用select 的时候*号表示全部字段;

注意:表的别名可以有效减少表明的长度;

mysql> select xx.name,fsb.id from xiaoxiong as xx,fengshenbang as fsb where xx.id=fsb.id;
+----------+------+
| name     | id   |
+----------+------+
| Zhangfei |    2 |
| zhaoyun  |    2 |
| liubei   |    2 |
| xiaoqiao |    2 |
| Zhangfei |    2 |
| zhaoyun  |    2 |
| liubei   |    2 |
| xiaoqiao |    2 |
+----------+------+
8 rows in set (0.00 sec)

总结:select返回的是对数据库的读操作,而insert、update、delete只返回此次操作影响的记录数;属于写操作。

二、读操作命令

2.1 select查看MySQL数据库的系统信息;

mysql> select now(); 查看当前日期和时间
mysql> select curdate(); 查看当前日期
mysql> select curtime();查看当前时间
mysql> select database();查看当前默认数据库
mysql> select version(); 查看当前mysql数据库版本
mysql> select user(); 查看当前登陆用户

2.2 show查看系统信息

mysql> show processlist;查看当前链接数
mysql> show variables\G; 查看当前系统信息
mysql> show global variables\G;查看全局配置信息
mysql> show global variables like ‘%version%‘ 查看系统版本号县官起哄%表示0个或多个未知字符
mysql> show global variables  like ‘%storage_engine%‘;查看当前默认存储引擎
mysql> show engines;查看当前所支持的存储引擎
mysql> show status;查看当前系统状态
mysql> show global status like ‘Thread%‘;查看当前线程数


三、数据库的备份和还原

3.1数据库备份:

语法:mysqldump -u<user> -p<password> sql_name>sql_name.sql

导入数据库:source /数据库备份文件·;

[root@node3 ~]# mysqldump -usys_neme -psys_password xiaoxiong > xiaoxiong.sql备份数据库
mysql> create database xiaoxiong;导入数据库之前需要先创建数据库;
mysql> use xiaoxiong;设置默认数据库
mysql> source /root/xiaoxiong.sql;导入已经备份的数据库
mysql> show tables;查看已经导入的数据库

3.2 通过select的结果导出到文本文件

mysql> select * into outfile ‘/tmp/xiaoxiong.txt‘ from xiaoxiong; /tmp目录是允许sql用户在其创建文件的
mysql> select ID,uuid,name,sex from xiaoxiong where sex like ‘M‘ into outfile ‘/tmp/bak_xiaoxiong.txt‘;
Query OK, 3 rows affected (0.00 sec)

四、逻辑运算符在MySQL中的使用

以下操作将在数据库book中操作

4.1 查看book数据库所包含的table,及getable的字段

mysql> desc category;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| bTypeId   | int(4)      | NO   | PRI | NULL    | auto_increment |
| bTypeName | varchar(40) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> mysql> desc books;
+------------+------------------------------------------------+------+-----+---------+----------------+
| Field      | Type                                           | Null | Key | Default | Extra          |
+------------+------------------------------------------------+------+-----+---------+----------------+
| bId        | int(4)                                         | NO   | PRI | NULL    | auto_increment |
| bName      | varchar(255)                                   | YES  |     | NULL    |                |
| bTypeId    | enum(‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘10‘) | YES  |     | NULL    |                |
| publishing | varchar(255)                                   | YES  |     | NULL    |                |
| price      | int(4)                                         | YES  |     | NULL    |                |
| pubDate    | date                                           | YES  |     | NULL    |                |
| author     | varchar(30)                                    | YES  |     | NULL    |                |
| ISBN       | varchar(255)                                   | YES  |     | NULL    |                |
+------------+------------------------------------------------+------+-----+---------+----------------+

8 rows in set (0.00 sec)

4.2 通过条件约束语句进行查询

查找出价格在40到70之间的书籍名字,并列出价格和出版社

mysql> select bName,price,publishing from books where price>40 and price<70;

4.3 算术运算符的应用

MySQL依旧支持大于>,小于<,等于=,不等于!=,大于等于>=和小于等于<=等

in 运算符适用于where表达式中,支持多个选择

语法:select * from tb_name where <字段> in (value1,value2,...)
mysql> select bName,price,publishing from books where price in (40,50,60,70);

not in与in作用相反;

mysql> select bName,price,publishing from books where price not in (40,50,60,70);


五、排序操作-order by

升序:MySQL中默认排序为升序输出asc

降序:order by "排序字段" desc

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

六、范围运算


[not] between ... and ...;
between ... and ...使用相当于大于并且等于表达式的运算,但表意不明;
例如:
mysql> select bName,price from books where price between 40 and 60 order by price desc;

七、模糊字段查询的使用-like

语法: <字段> [not]like ‘通配符‘ 其中%表示多个字符的意思

例如:查找出表books中带有网页字样的书名,并打印出来

mysql> select bName from books where bName like ‘%网页%‘;
+---------------------------------------------------------+
| bName                                                   |
+---------------------------------------------------------+
| 网页样式设计-CSS                                       |
| Dreamweaver 4网页制作                                   |
| Fireworks 4网页图形制作                                 |
| 网页界面设计艺术教程                                    |
| Frontpage 2000& ASP 网页设计技巧与网站维护             |
+---------------------------------------------------------+
5 rows in set (0.00 sec)


八、子查询的应用

语法:在查询语句中select的where条件中又出现了select查询的嵌套语句。

mysql> select bName,bTypeId,price from books where bTypeId=(select bTypeId from category where bTypeName=‘黑客‘);
+--------------------------+---------+-------+
| bName                    | bTypeId | price |
+--------------------------+---------+-------+
| 黑客与网络安全           | 6       |    41 |
| 黑客攻击防范秘笈         | 6       |    44 |
+--------------------------+---------+-------+
2 rows in set (0.00 sec)

九、限制查询结果显示的条目

语法:select * from tb_name LIMIT [n,]m;

limit子句,可以用于select语句返回指定的记录数,LIMIT接收一个或两个参数,参数必须是一个整数常量。如果给定连个参数则第一个参数指定的是第一个返回记录行的偏移量,第二个则表示指定返回记录行的最大数目。初始行记录偏移量为0;

查找处books表中第5行到第10行的记录,共6个数字,

mysql> mysql> select * from books limit 4,6;
+-----+-------------------------------+---------+-----------------------------------+-------+------------+-----------+------------+
| bId | bName                         | bTypeId | publishing                        | price | pubDate    | author    | ISBN       |
+-----+-------------------------------+---------+-----------------------------------+-------+------------+-----------+------------+
|   5 | 黑客攻击防范秘笈              | 6       | 北京腾图电子出版社                |    44 | 2003-06-29 | 赵雷雨    | 7120000233 |
|   6 | Dreamweaver 4入门与提高       | 2       | 清华大学出版社                    |    44 | 2004-06-01 | 岳玉博    | 7505397699 |
|   7 | 网页样式设计-CSS             | 2       | 人民邮电出版社                    |    45 | 2002-03-01 | 张晓阳    | 7505383663 |
|   8 | Internet操作技术              | 7       | 清华大学出版社                    |    45 | 2002-02-01 | 肖铭      | 7121003023 |
|   9 | Dreamweaver 4网页制作         | 2       | 清华大学出版社                    |    45 | 2004-04-01 | 黄宇      | 7505380796 |
|  10 | 3D MAX 3.0 创作效果百例       | 3       | 北京万水电子信息出版社            |    45 | 2002-09-01 | 耿影      | 7505380796 |
+-----+-------------------------------+---------+-----------------------------------+-------+------------+-----------+------------+

9.2查看所有书籍中价格最低的书名和价格

mysql> select bName,price from books order by price limit 1;
+-----------------------+-------+
| bName                 | price |
+-----------------------+-------+
| 网站制作直通车        |    34 |
+-----------------------+-------+
1 row in set (0.00 sec)


十、综合应用

10.1 显示出bId,bName,bTypeId内容,条件:价格要比电子工业出版社中的书中最便宜的书还要便宜;

mysql> select bId,bName,bTypeId from books where price < (select price from books where publishing="电子工业出版社"order by price asc limit 1);
+-----+--------------------------------------------------------+---------+
| bId | bName                                                  | bTypeId |
+-----+--------------------------------------------------------+---------+
|   1 | 网站制作直通车                                         | 2       |
|   2 | 黑客与网络安全                                         | 6       |
|   3 | 网络程序与设计-asp                                    | 2       |
|   4 | pagemaker 7.0短期培训教程                              | 9       |
|   5 | 黑客攻击防范秘笈                                       | 6       |
|   6 | Dreamweaver 4入门与提高                                | 2       |
|   7 | 网页样式设计-CSS                                      | 2       |
|   8 | Internet操作技术                                       | 7       |
|   9 | Dreamweaver 4网页制作                                  | 2       |
|  10 | 3D MAX 3.0 创作效果百例                                | 3       |
|  11 | Auto CAD职业技能培训教程                               | 10      |
|  12 | Fireworks 4网页图形制作                                | 2       |
|  13 | 自己动手建立企业局域网                                 | 8       |
|  14 | 页面特效精彩实例制作                                   | 2       |
|  15 | 平面设计制作整合案例详解-页面设计卷                   | 2       |
|  16 | Illustrator 10完全手册                                 | 9       |
|  17 | FreeHand 10基础教程                                    | 9       |
|  18 | 网站设计全程教程                                       | 2       |
|  19 | 动态页面技术-HTML 4.0使用详解                         | 2       |
|  20 | Auto CAD 3D模型大师                                    | 10      |
|  21 | Linux傻瓜书                                            | 4       |
|  22 | 网页界面设计艺术教程                                   | 2       |
|  23 | Flash MX 标准教程                                      | 2       |
|  24 | Auto CAD 2000 应用及实例基集锦                         | 10      |
|  25 | Access 2000应用及实例基集锦                            | 1       |
mysql> select bName,price from books where price<(select price from books where publishing="电子工业出版社" order by price asc limit 0,1);

10.2 多子句查询-all

与列出的最小或最大值进行比较

mysql> select * from books where price<all(select price from books where publishing="电子工业出版社");






本文出自 “小熊运维” 博客,请务必保留此出处http://maoxiaoxiong.blog.51cto.com/11705634/1982162

4、MySQL查询语句01

标签:数据库查询   备份   还原   逻辑运算   

原文地址:http://maoxiaoxiong.blog.51cto.com/11705634/1982162

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!