一、MySql常用命令

本文参考《MySQL必知必会》,对其中的常用命令进行了总结。

数据中预置了6张表,并分别预埋了一定的实验数据,表结构分别如下:

1、简单select语句

1.1、基本select语句

选择一列

select prod_name from products;

选择多列,每列用逗号“,”隔开

select prod_id, prod_name, prod_price from products;

选择所有列,使用通配符*,非必要情况最好不用通配符,会降低性能。

select * from products;

1.2、 distinct:筛选去重

筛选返回的vend_id,去除重复值,每个值只返会一个

select distinct vend_id from products;

如果distinct后面跟了多列,那么是将多列组合看做一个整体,将这个组合整体进行去重,每个不同的值返回一次。如下,vend_id和prod_price两列为一个组合,当组合里面任意一个字段不同时,认为这个组合整体不是重复的。

select distinct vend_id, prod_price from products;

1.3、limit限制结果数量

 只选出表中的前5行

select * from products limit 5;

选出表中的4行,从索引为5的行开始选(表中第一行索引为0,所以是从第6行开始选),第一个数为开始的位置,第二个数为开始的行数:

select * from products limit 5, 4;

 1.4、使用全限定名称

可以在列名和表名前面加上全限定名称,列名前面加上表名,表名前面加上数据库名:

select products.prod_id from products;
select products.prod_id from mysql_must_know.products;

 1.5、order by排序子句

// order by默认是按升序排列
select prod_name from products order by prod_name;

// 虽然没有选出prod_id,但是以它排序也是合法的
select prod_name from products order by prod_id; 

// 根据多个字段进行排序,首先通过价格排序,价格相同时,再根据名称排序
select prod_id, prod_name, prod_price from products order by prod_price, prod_name;

 desc降序排序:desc关键字只作用于语句中位于其前面的那一个字段,如果多个字段都要按照降序排序,就要用多个desc关键字。

// 按照价格降序排列
select prod_id, prod_name, prod_price from products order by prod_price desc;


// 根据多个字段进行排序,首先按照价格降序排序,价格相同时,再根据名称排序的升序排序
select prod_id, prod_name, prod_price from products order by prod_price desc, prod_name;

 组合limit和order by语句可以选出某一特定的列,例如选出价格最高的一列:

select * from products order by prod_price desc limit 1;

1.6、where 过滤条件

select * from products where prod_price = 2.5;

// 同时使用where和order by时,order by放在where子句的后面
select * from products where prod_price > 2.5 order by prod_price;

// between语句的区间是[],即两个边界的值都要取
select * from products where vend_id between 1002 and 1005;
// 等价于
select * from products where vend_id >= 1002 and vend_id <= 1005;

 

 字符串类型使用=比较时,mysql默认是是忽略大小写的。

选择某个字段不为某个值时,如果某一行该字段为空,那么是选不出来的,如下:

 为了选出为null的记录,需要使用以下条件:

select * from products where prod_price is null;
select * from products where prod_price is not null;

where中可以有多个条件,使用or、and进行连接,默认情况下,and的优先级高于or,如下示例:

由于and的优先级较高,所以这个查询语句的实际含义是:查询出vend_id为1002且价格不低于10的商品,或者vend_id为3的所有商品。

select * from products where vend_id = 1002 or vend_id = 1003 and prod_price >= 10.0;

结果如下所示:

如果期望查询vend_id为1002或1003,并且价格不低于10的记录 ,需要加上括号:

select * from products where (vend_id = 1002 or vend_id = 1003) and prod_price >= 10.0;

为了保证查询结果符合预期,在有多个or/and时,都应该使用括号来标明,避免逻辑出错。

使用in操作符

select * from products where vend_id in (1002, 1003);

not操作符:有且只有一个作用,用来否定where语句中not操作符后面的各种条件,例如

select * from products where vend_id not in (1002, 1003);

1.7、like操作符

百分号(%)通配符:匹配任意字符出现任意次数,也就是说%匹配0个、1个或者多个任意字符

注意,不能匹配null

select prod_id, prod_name from products where prod_name like "%jet%";

 下划线(_)通配符:只能匹配单个字符,不能匹配0个或多个字符

select prod_id, prod_name from products where prod_name like "_ ton anvil";

 

注意:不要过度使用通配符,会降低性能,尤其是当通配符处于搜索模式的开头时,如以上两个示例,速度是最慢的。

1.8、regexp 正则表达式匹配

 regexp表示,要筛选的字符串的一个子串只要匹配后面的正则表达式,就算是符合条件,如下示例。与like的区别就在于此,like是要筛选的字符串的整体符合后面的正则表达式才行。

select prod_id, prod_name from products where prod_name regexp "1000";

 

 regexp关键字后面可以跟其他的正则表达式,比如:

这里的"|"表示取或的意思,还有其他的正则表达式,在此不再赘述,需用用到时可以详查。

select prod_id, prod_name from products where prod_name regexp "1000|2000";

select prod_id, prod_name from products where prod_name regexp "1|2|3 Ton";

2、函数

2.1、concat函数:连接字符串

select concat(vend_name, "(", vend_country, ")") from vendors;

 

使用别名:

select concat(vend_name, "(", vend_country, ")") as vend_title from vendors;

 执行算术计算:

select prod_id, quantity, item_price,  quantity * item_price as total_price from orderitems;

2.2、常见字符串函数

例:upper函数将字符串转换为大写:

select vend_name, upper(vend_name) as upper_vend_name from vendors limit 1;

 

 

 

 2.3、日期和时间处理函数

首先需要注意的是MySQL使用的日期格式。无论什么时候指定一个日期,不管是插入或更新表值还是用WHERE子句进行过滤,日期格式最好使用yyyy-mm-dd。

select * from orders;

 下面这种方式的筛选不靠谱的,order_date中的时间不是00:00:00时,使用下面这种方式是筛选不出来的。

select * from orders where order_date = '2005-09-01';

 

order_date的类型时datetime,为了保证只使用日期就能筛选出来,需要使用date函数:

select * from orders where date(order_date) = '2005-09-01';

 

 使用date函数之后,将datetime类型转换为了date类型,这样无论当其时间是否为00:00:00,都保证能够筛选出来。

 

 2.4、数值函数

 

 

 

 

Guess you like

Origin blog.csdn.net/sun_lm/article/details/120102905