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

[学习记录]MySQL之初级查询语句(select,where)

时间:2018-06-26 11:09:00      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:where   not   com   span   括号   过滤   条件   学习记录   开始   

 

标准的查询语句如下

select   列名1,列名2,列名3   from   表名

该语句将返回该表中对应列的所有行


 

如果想要检索表中所有列则可以使用*

select * from 表名a

 

由于表中非码属性允许重复,所以发现检索结果中有重复项,可以通过distinct过滤

select distinct 列名 from 表名f

 

如果只关心检索结果的前若干行,可以使用limit子句

select 列名  from 表名  limit  a,b

 意思为输出从第a行开始一共b行元素,如果不提供a则从第1行开始


如果需要对检索结果进行排序,可以使用order by 子句

select 列名a,列名b  from  表名  order by 列名b;
select 列名a,列名b  from  表名  order by  列名b desc;

即根据列名b进行排序,加上desc为逆向排序,也可以在列名后面通过逗号分隔加上另一个排序列。例如  order by 列名b desc,列名c


通过where实现数据过滤

select  列名  from  表名  where   name=a

需要注意如果需要对结果进行排序,where 必须在order by 之前

where支持的条件操作如图

技术分享图片

例如   select   product from products where price between 5 and 10

where还支持对空值的判断

select  product from products where  attr is null


关于where更高级的用法

where 支持对于多个条件之间的and 和 or运算,但注意and的优先级高于or,如有必要请加括号

此外where还支持in  和  not

select  student  from  stus  where  age in (18,20)

select  student  from  stus  where  age not  in (18,20)

值得注意的是这里第一条语句匹配的是age=18或20,第二条匹配的是age既不等于18也不等于20.

而不是18到20.

 

[学习记录]MySQL之初级查询语句(select,where)

标签:where   not   com   span   括号   过滤   条件   学习记录   开始   

原文地址:https://www.cnblogs.com/trickofjoker/p/9227054.html

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