SQL查询语句

SQL查询语句

以下是表student的数据

idnameage
0李四18
1阿马3
2阿李23
3阿人34

1.简单的查询语句(DQL)

语法格式:

​ select 字段1,字段2,字段3,…from 表名;

提示:

​ 1.任何一条sql语句以";"结尾

​ 2.sql语句不区分大小写

  • 查询他们的年纪?
select age from student;

±----+
| age |
±----+
| 18 |
| 3 |
| 23 |
| 34 |
±----+

  • 给他们的年纪乘以2 ?
select age*2 from student;

±------+
| age*2 |
±------+
| 36 |
| 6 |
| 46 |
| 68 |
±------+

  • 给查询结果的列重命名?
select age*2 as newage from student;

±-------+
| newage |
±-------+
| 36 |
| 6 |
| 46 |
| 68 |
±-------+

  • as关键字可以省略吗?
select age*2  newage from student;

±-------+
| newage |
±-------+
| 36 |
| 6 |
| 46 |
| 68 |
±-------+

可以省略

  • 注意:标准sql语句中要求字符串使用单引号括起来。虽然mysql支持双引号,尽量别用。

  • 查询所有的字段?

select * from student;

±—±-----±----+
| id | name | age |
±—±-----±----+
| 0 | 李四 | 18 |
| 1 | 阿马 | 3 |
| 2 | 阿李 | 23 |
| 3 | 阿人 | 34 |
±—±-----±----+

2.条件查询

语法格式

​ select

​ 字段,字段…

​ from

​ 表名

​ where

​ 条件;

执行顺序:先from,然后where,最后select

简单条件运算符:> < = != <> >= <=

  • 查询age=18的学生的name?
select name from student where age=18;

±-----+
| name |
±-----+
| 李四 |
±-----+

  • 查询name为阿马的age?
select age from student where name='阿马';
//字符串使用单引号括起来

±----+
| age |
±----+
| 3 |
±----+

  • 找出age大于18的name
select  name,age from student where age>18;
select  name,age from student where age<18;//找出agex小于18的name
select  name,age from student where age>=18;//找出age大于等于18的name
select  name,age from student where age=18;//找出age等于18的name
select  name,age from student where age<=18;//找出age小于等于18的name
找出age大于18的name

±-----±----+
| name | age |
±-----±----+
| 阿李 | 23 |
| 阿人 | 34 |
±-----±----+

  • 找出age不等于18的name
select name,age from student where age<>18;
select name,age from student where age!=18;

±-----±----+
| name | age |
±-----±----+
| 阿马 | 3 |
| 阿李 | 23 |
| 阿人 | 34 |
±-----±----+

条件查询between…and…

  • 找出age在20和30之间的name
select name,age from student where age >=20 and age<=30;
select name,age from student where age between 20 and 30;

注意:between…and… 对数字来说是闭区间[20,30],且必须是左小右大;between…and… 除了可以使用在数字方面外,还可以使用在字符串方面,但是区间却是左闭右开。

±-----±----+
| name | age |
±-----±----+
| 阿李 | 23 |
±-----±----+

条件查询is null和is not null

±—±-----±----+
| id | name | age |
±—±-----±----+
| 0 | 李四 | 18 |
| 1 | 阿马 | 3 |
| 2 | 阿李 | 23 |
| 3 | 阿人 | 34 |
| 4 | NULL | 20 |
±—±-----±----+

在数据库中null不是一个值,代表什么也没有,为空。

空不是一个值,不能用等号衡量。

必须使用is null或者is not null

select id,name from student where name is null;

±—±-----+
| id | name |
±—±-----+
| 4 | NULL |
±—±-----+

select id,name from student where name is not null;

±—±-----+
| id | name |
±—±-----+
| 0 | 李四 |
| 1 | 阿马 |
| 2 | 阿李 |
| 3 | 阿人 |
±—±-----+

条件查询and和or的优先级

±—±-----±----±----+
| id | name | age | sex |
±—±-----±----±----+
| 0 | 李四 | 18 | 男 |
| 1 | 阿马 | 3 | 男 |
| 2 | 阿李 | 23 | 男 |
| 3 | 阿人 | 34 | 女 |
| 4 | NULL | 20 | 女 |
| 5 | 惹我 | 15 | 女 |
| 6 | 几款 | 17 | 女 |
±—±-----±----±----+

查找出age>18的男生或者女生的name

select name,sex,age from student where age>18 and sex='男' or sex='女';

但是该sql查询出来的结果并不符合要求,执行结果如下:
| name | sex | age |
±-----±----±----+
| 阿李 | 男 | 23 |
| 阿人 | 女 | 34 |
| NULL | 女 | 20 |
| 惹我 | 女 | 15 |
| 几款 | 女 | 17 |
±-----±----±----+

修改下SQL语句,添加上括号。如下:

select name,sex,age from student where age>18 and (sex='男' or sex='女');

±-----±----±----+
| name | sex | age |
±-----±----±----+
| 阿李 | 男 | 23 |
| 阿人 | 女 | 34 |
| NULL | 女 | 20 |
±-----±----±----+

分析

从上面的场景中,问题的关键就在于AND和OR的执行顺序问题。
查阅资料,关系型运算符优先级高到低为:AND >OR
如果where 后面有OR条件的话,则OR自动会把左右的查询条件分开。
就如上面场景中的第一条语句,他的查询条件分为两部分(或):

1、age>18 and sex='男'
2、sex='女'

条件查询in

in等同于or:

找出age=18和age=20的name

select name from student where age=18 or age=20;
select name from student where age in (18,20);

±-----+
| name |
±-----+
| 李四 |
| NULL |
±-----+

注意:

in后面的不是区间,是具体的值

not in:不在这几个值当中。

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

±-----+
| name |
±-----+
| 阿马 |
| 阿李 |
| 阿人 |
| 惹我 |
| 几款 |
±-----+

模糊查询like

一般模糊查询语句如下:

SELECT 字段 FROM 表 WHERE 某字段 Like 条件

其中关于条件,SQL提供了四种匹配模式:

1,% :表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。

比如 SELECT * FROM [user] WHERE u_name LIKE ‘%三%’

将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来。

另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE ‘%三%’ AND u_name LIKE ‘%猫%’

若使用 SELECT * FROM [user] WHERE u_name LIKE ‘%三%猫%’
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。

2,_ : 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:

比如 SELECT * FROM [user] WHERE u_name LIKE ‘
只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;

再比如 SELECT * FROM [user] WHERE u_name LIKE ‘三__’;
只找出“三脚猫”这样name为三个字且第一个字是“三”的;

3,[ ] :表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。

比如 SELECT * FROM [user] WHERE u_name LIKE ‘[张李王]三’
将找出“张三”、“李三”、“王三”(而不是“张李王三”);

如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e”
SELECT * FROM [user] WHERE u_name LIKE ‘老[1-9]’
将找出“老1”、“老2”、……、“老9”;

4,[^] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。

比如 SELECT * FROM [user] WHERE u_name LIKE ‘[^张李王]三’
将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;

SELECT * FROM [user] WHERE u_name LIKE ‘老[^1-4]’;
将排除“老1”到“老4”,寻找“老5”、“老6”、……

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值