设为首页 加入收藏

TOP

常用Mysql语句
2017-11-21 08:24:16 】 浏览:54
Tags:常用 Mysql 语句

创建了一个表名字叫yingXiong,并填充相应的数据,以上操作在navicat视图工具里面:

\
\

1、查询整个表格

slelect * from yingXiong;

2、查询某一个或多个字段的值

select y_name from yingXiong;

select y_name,y_sex from yingXiong;---要查询的字段与字段之间用逗号(,)分隔

3、条件查询及逻辑运算符

select * from yingXiong where y_age<30;

select * from yingXiong where y_shuchu='800';

注意:逻辑运算符:< = > <=>=!= 和<>相同,条件语句中条件的值需要加单引号('')

4、多条件查询

select * from yingxiong where y_sex='男' and y_age>'30';

select * from yingxiong where y_sex='男' and y_age>'30' and y_type='adc';

select * from yingxiong where y_sex='女' or y_type='adc';---两个条件只要满足一个即可

5、排序查询

select * from yingxiong order by y_age;

order by关键字默认正排序;

如果要倒排序,需要加入desc关键字:select * from yingxiong order by y_age desc;

正序排序也可以用(asc)关键字:select * from yingxiong order by y_age asc;

6、区间查询

select * from yingxiong where y_age >='18' and y_age<='30';---查询18~30年之间的英雄信息

select * from yingxiong where y_age between '18' and '30';

上面两条语句查询效果相同

between ... and ...

7、集合方式查询

select * from yingxiong where y_age in(18,25);

in关键字表示集合,在in后面的括弧里面填写集合范围

8、模糊查询

select * from ying xiong where y_name like '%李%';

使用Like关键字,表示模糊,后面跟'%模糊的内容%',前面为要根据哪一个字段进行模糊查询

注意:'%李%',表示只要该字段中包含了李字就查询出来

'李%',表示只要该字段首字符包含了李就查询出来

'%李',表示只要该字末尾字符包含了李就查询出来

9、分页查询

select * from yingxiong limit 1,4;

limit关键字表示分页,在关键字之后跟两个数字0,4 ,0表示从下标为几的数字开始,4表示当前分页中显示多少条数据

中间用逗号分隔(英文逗号)

limit关键字后面如果为一个数字,则表示当前分页显示多少条数字,如为7就是显示7条语句

10、简单的组合查询

select * from yingxiong where y_age<'30' and y_name like '%乔%' order by y_age limit 0,2;

注意关键字组合的顺序,limit关键字一定是放在语句最后面的

11、命名别名

select y_name as '姓名' from yingxiong;

as在关键字表示命名别名,关键字之前为要命名的字段或者表,之后是别名

select y_name '姓名' from yingxiong;

as关键字也可以省略,但原名和别名之间必须有空格

12、常用的聚合函数

a,求最大值:select max(y_age) from yingxiong;

max关键字表示最大

b,求最小值:select min(y_age) from yingxiong;

min表示最小

c,求平均值:select avg(y_age) from yingxiong;

avg关键字表示平均

d,求和:select sum(y_age) from yingxiong;

sum关键字表示求和

e,统计共有多少条数据:select count(y_age) from yingxiong;

count关键字表示求总数,

也可以这样写语句:select count(*) from yingxiong;

13、子条件查询

select * from yingxiong where y_age=(select max(y_age) from yingxiong);

将一个查询结果当做条件来使用

14、分组查询

select y_type,sum(y_age)from yingxiong group by y_type;

group by关键字表示分组,按照某一个字段的值进行分组,相同的值组成一组。

注意:分组函数一般和聚合函数一起使用

15、分组加条件查询

select y_type,sum(y_age) as ageHe from yingxiong group by y_type having ageHe>'60';

having关键字表示条件,为分组函数的专业条件关键字

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MySql数据库逻辑架构讲解 下一篇Mysql的四种隔离级别讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目