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

mysql常用操作

时间:2016-07-29 18:53:47      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

一.order by 列名

例如:

1.//按id排序,默认是按升序排列的,

select * from table order by id;等同于select * from table order by id ASC;

2.按降序排列:

select * from table order by desc;

3.按多个字段排序:

例如:select * from table order by id,name;

二.where 子句操作符

技术分享

 

三.范围值检查:

关键字:between    and

例如:

select * from table where id between 1 and 3;

 

四.控制检查

关键字:IS NULL

例如://查出所有name字段为null的值

select * from table where name is null;

 

5.组合where子句

5.1. AND操作符

例如://查找id等于1并且名字为lisi的人

select * from table where id=1 and name=‘lisi‘;

5.2.OR操作符

例如://查找id=2或者名字为lisi的用户

select * from table where id=2 OR name=‘lisi‘;

5.3.AND与OR关键字组合

示例://ADN在计算中的优先级高于OR,如果不加()括号会先算AND部分

select * from table where (id=2 OR id=3) AND name=‘lisi‘;

5.4.IN操作符--用来指定条件范围的

例如://查询id为1,3,5的人,并按name进行降序排列

select * from table where id IN (1,3,5) order by name desc;

5.5.NOT操作符--不等于

例如://id不等于1

select * from table where not id=1 order by name desc;

 

6.用通配符进行过滤

6.1.like通配符

6.1.1.%通配符

示例:1://模糊查询,查询name字段里以li开头的所有信息

select * from table where name like ‘li%‘;

示例2:查询name字段里以si结尾的所有信息:

select * from table where name like ‘%si‘;

示例3:查询name字段里以‘x‘开头和以‘y‘结尾的所有信息

select * from table where name like ‘x%y‘;

6.1.2.下划线通配符"_"

与%号作用一样,但是下划线通配符只能匹配单个字符,而不能匹配多个字符,下划线‘_‘可以在任何位置

示例:

select * from test where name like ‘lis_‘;

6.1.3.方括号"[]"通配符

示例:

select * from table where name like ‘[zlw]%‘ order by name desc;

7.通配符的使用技巧

技术分享

 

mysql常用操作

标签:

原文地址:http://www.cnblogs.com/liyanbin/p/5719118.html

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