引言
本篇博客对 MySQL 数据库的一些命令进行分享。
where 语句
基本语法规则:
select field1, field2,...fieldN from table_name1, table_name2...
[where condition1 [and [or]] condition2.....
说明:where 子句中指定任何条件;也可以使用 and 或者 or 指定一个或多个条件;where 子句经常与SQL 的 select、delete 或者 update 命令一起使用;where 子句类似于程序语言中的 if 条件,根据 MySQL 表中的字段值来读取指定的数据。
实例:
以下实例将读取 user 表中 user_name 字段值为 ZHANGSAN 的所有记录:
select * from user where binary user_name='ZHANGSAN';
binary 关键字来设定 where 子句的字符串比较是区分大小写的。
like 语句
基本语法规则:
select field1, field2,...fieldN
from table_name
where field1 like condition1 [and [or]] filed2 = 'somevalue'
说明: 使用l ike 子句来代替等号 =;like 通常与 % 一同使用,类似于一个元字符的搜索;可以使用 and 或者 or 指定一个或多个条件;也可以在 delete 或 update 命令中使用 where…like 子句来指定条件。
实例:
以下是我们将 user 表中获取 user_name 字段中以 SAN 为结尾的的所有记录:
select * from user where user_name LIKE '%SAN';
like 匹配/模糊匹配,会与 % 和 _ 结合使用。
'%a' //以a结尾的数据
'a%' //以a开头的数据
'%a%' //含有a的数据
'_a_' //三位且中间字母是a的
'_a' //两位且结尾字母是a的
'a_' //两位且开头字母是a的
union 语句
基本语法规则:
select expression1, expression2, ... expression_n
from tables
[where conditions]
union [all | distinct]
select expression1, expression2, ... expression_n
from tables
[where conditions];
参数:
- expression1, expression2, … expression_n: 要检索的列;
- tables: 要检索的数据表;
- where conditions: 可选, 检索条件;
- distinct: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响;
- all: 可选,返回所有结果集,包含重复数据。
实例:
下面的 SQL 语句从 “Websites” 和 “apps” 表中选取所有不同的country(只有不同的值):
select country from Websites
union
select country from apps
order by country;
下面的 SQL 语句使用 union all 从 “Websites” 和 “apps” 表中选取所有的country(也有重复的值):
select country from Websites
union all
select country from apps
order by country;
下面的 SQL 语句使用 union all 从 两张表"Websites" 和 “apps” 中选取所有的中国(CN)的数据(也会有重复的值):
select country, user_name from Websites
where country='CN'
union all
select country, app_name FROM apps
where country='CN'
order by country;
排序
order by 基本语法规则:
select field1, field2,...fieldN table_name1, table_name2...
order by field1, [field2...] [ASC [DESC]]
说明:可以设定多个字段来排序;可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 默认情况下,它是按升序排列。
实例:
下面的 SQL 语句使用 order by 把表user按照国家升序排列:
select * from user order by country ASC;
下面的 SQL 语句使用 order by 把表user按照国家降序排列:
select * from user order by country DESC;
分组
group by 基本语法规则:
select column_name, function(column_name)
from table_name
where column_name operator value
group by column_name;
实例演示:
创建 test.sql 文件,并导入 mysql:
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Database structure for `test`
-- ----------------------------
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE test;
use test;
-- ----------------------------
-- Table structure for `employee_tbl`
-- ----------------------------
DROP TABLE IF EXISTS `employee_tbl`;
CREATE TABLE `employee_tbl` (
`id` int(11) NOT NULL,
`name` char(10) NOT NULL DEFAULT '',
`date` datetime NOT NULL,
`singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '登录次数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `employee_tbl`
-- ----------------------------
BEGIN;
INSERT INTO `employee_tbl` VALUES ('1', '小明', '2016-04-22 15:25:33', '1'), ('2', '小王', '2016-04-20 15:25:47', '3'), ('3', '小丽', '2016-04-19 15:26:02', '2'), ('4', '小王', '2016-04-07 15:26:14', '4'), ('5', '小明', '2016-04-11 15:26:40', '4'), ('6', '小明', '2016-04-04 15:26:54', '2');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
导入成功后,执行以下 SQL 语句:
set names utf8;
SELECT * FROM employee_tbl;
使用 group by 语句将数据表按名字进行分组,并统计每个人有多少条记录:
select name, count(*) from employee_tbl group by name;
with rollup
在这里,使用with rollup 可以实现在分组统计数据基础上再进行相同的统计(sum,avg,count…):
select name, sum(singin) as singin_count from employee_tbl group by name with rollup;
也可以配合使用 coalesce 来设置一个可以取代 Null 的名称,coalesce 语法:
select coalesce(a,b,c);
参数说明:如果 a==null
,则选择 b;如果b==null
,则选择 c;如果a!=null
,则选择 a;如果a b c
都为 null ,则返回为 null(没意义)。
以下实例中如果名字为空我们使用总数代替:
select coalesce(name, '总数'), sum(singin) as singin_count from employee_tbl group by name with rollup;
连接 join
可以在 select, update 和 delete 语句中使用 Mysql 的 join 来联合多表查询。
join 按照功能大致分为如下三类:
inner join(内连接,或等值连接):获取两个表中字段匹配关系的记录。
left join(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
right join(右连接): 与 left join 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
实例:
inner join
使用 MySQL 的 inner join (也可以省略 inner 使用 join,效果一样)来连接以上两张表来读取 user_tbl表中所有user_author字段在 count_tbl 表对应的 user_count字段值:
select a.user_id, a.user_author, b.user_count from user_tbl a inner join count_tbl b on a.user_author = b.user_author;
上述语句等价于
select a.user_id, a.user_author, b.user_count FROM user_tbl a, count_tbl b where a.user_author = b.user_author;
left join
MySQL left join 与 join 有所不同。 MySQL left join 会读取左边数据表的全部数据,即便右边表无对应数据。
select a.user_id, a.user_author, b.user_count from user_tbl a left join count_tbl b on a.user_author = b.user_author;
right join
MySQL right join 会读取右边数据表的全部数据,即便左边边表无对应数据。
select a.user_id, a.user_author, b.user_count from user_tbl a right join count_tbl b on a.user_author = b.user_author;
总结
以上讲解了 MySQL 数据库常用命令 where、like、union、order by、group by、join,并给出了一些基本的实例,开发人员多多操作,勤加练习,才能学以致用。
如在操作过程中遇到问题,欢迎及时交流。