MySQL查询语句⼤全(列举所有常⽤查询⽅式)
⽂章⽬录
前提条件
⾸先创建SQL测试⽂件,其中创建了⼀个库两个表,⽤于测试。
⽂件名称:test.sql
/*------------员⼯信息库-------------*/
create database staff;
use staff;
create table yunwei(id int not null primary key,name char(4)not null,age tinyint(3)unsigned not null,sex enum('man','woman')not null); create table caiwu(id int not null primary key,name char(4)not null,age tinyint(3)unsigned not null,sex enum('man','woman')not null);
insert into yunwei values(1,'张三',20,'man'),(2,'张四',21,'man'),(3,'张莉',22,'woman'),(4,'张五',23,'man'),(
5,'张六',24,'man'),(6,'张丽',25,'woman'); insert into caiwu values(1,'李⼀',24,'man'),(2,'李莉',23,'woman'),(3,'李⼆',22,'man'),(4,'李三',21,'man'),(5,'李四',20,'man'),(6,'李丽',19,'woman');
将SQL⽂件导⼊到数据库中
mysql < test.sql
导⼊完成后,在staff库下有两张表,分别是yunwei和caiwu
mysql>select*from staff.yunwei;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|1|张三|20| man  |
|2|张四|21| man  |
|3|张莉|22| woman |
|4|张五|23| man  |
|5|张六|24| man  |
|6|张丽|25| woman |
+----+--------+-----+-------+
mysql>select*from staff.caiwu;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|1|李⼀|24| man  |
|2|李莉|23| woman |
|3|李⼆|22| man  |
|4|李三|21| man  |
|5|李四|20| man  |
|6|李丽|19| woman |
+----+--------+-----+-------+
android开发培训费用简单查询
直接查询
语法:select 字段 from 表名;
最简单的⼀种查询⽅式,可查看多个字段或整张表。
例如:查询运维表姓名及年龄
mysql>select name,age from yunwei;
+--------+-----+
| name  | age |
+--------+-----+
|张三|20|
|张四|21|
|张莉|22|
|张五|23|
|张六|24|
|张丽|25|
+--------+-----+
条件查询
关键字为where,通常位于表名后⾯
语法:select 字段 from 表名 where 条件;
根据条件,查询指定条件的字段。
例如:查询财务表中年龄为20的员⼯
mysql>select*from caiwu where age=20;
+----+--------+-----+-----+
| id | name  | age | sex |
+----+--------+-----+-----+
|5|李四|20| man |
+----+--------+-----+-----+
模糊查询
关键字是like,通常位于条件字段后⾯
语法:select 字段 from 表名 where 字段 like ‘%数据%’;
通过输⼊具体的数据,来对记录进⾏查询
例如:模糊查询caiwu表中姓名字段还有三的记录
mysql>select*from caiwu where name like'%三%';
+----+--------+-----+-----+
| id | name  | age | sex |
+----+--------+-----+-----+
|4|李三|21| man |
+----+--------+-----+-----+
算数运算符
运算符⼀般配合逻辑运算符⼀起使⽤,可以使条件限制更加具体。
符号作⽤
>⼤于
<⼩于
=等于
!=不等于
ps水平居中对齐不能用<>与!=同义,不等于
>=⼤于等于
<=⼩于等于
通过逻辑运算符可以将条件限制在⼀个范围内。
例如:查看caiwu表中id不等于1的记录
mysql>select*from caiwu where id!=1;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|2|李莉|23| woman |
|3|李⼆|22| man  |
|4|李三|21| man  |
|5|李四|20| man  |
|6|李丽|19| woman |
+----+--------+-----+-------+
逻辑运算符
可以将查询的单个条件改为多个条件或满⾜多个条件中的⼀个。
符号作⽤
and与,同时满⾜多个条件
or或,满⾜多个条件中的⼀个即可
not否,不满⾜条件
和算数运算符⼀起使⽤,可以将条件更加具体。
例如:查询yunwei表中性别为⼥,或年龄为23的记录
mysql>select*from yunwei where sex='woman'or age=23;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|3|张莉|22| woman |
|4|张五|23| man  |
|6|张丽|25| woman |
+----+--------+-----+-------+
in与not in运算符
关键字为in,通常位于条件字段后⾯
语法:select 字段 from 表名 where 字段 in (列表);
符号作⽤
in在⼀个条件列表中not in不在⼀个条件列表中例如:查询yunwei表中,年龄为22-25之间的记录
mysql>select*from yunwei where age in(22,23,24,25);
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|3|张莉|22| woman |
|4|张五|23| man  |
|5|张六|24| man  |
|6|张丽|25| woman |
+----+--------+-----+-------+
排序查询
关键字为order by与asc,desc,通常位于表名之后
排序分为两种,升序(asc)和降序(desc)
语法:select 字段 from 表名 order by 字段 排序⽅式;
例如:将caiwu表中记录按年龄从⼤到⼩查询
mysql>select*from caiwu order by age desc;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|1|李⼀|24| man  |
|2|李莉|23| woman |
|3|李⼆|22| man  |
|4|李三|21| man  |
|5|李四|20| man  |
|6|李丽|19| woman |
+----+--------+-----+-------+
⾼级查询
范围运算
关键字为between…and…,通常位于条件字段后⾯。
语法:select 字段 from 表名 where 字段 between 范围1 and 范围2;
也是⽤来限制查询范围,作为算数运算符的⼀种替换。
例如:查询caiwu表中,年龄为21-23的记录,使⽤算数运算符表⽰为age>=21 and age<=23,使⽤范围运算啧表⽰为age between 21 and 23。
mysql>select*from caiwu where age >=21and age<=23;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|2|李莉|23| woman |
|3|李⼆|22| man  |
|4|李三|21| man  |
+----+--------+-----+-------+
mysql>select*from caiwu where age between21and23;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|2|李莉|23| woman |
|3|李⼆|22| man  |
|4|李三|21| man  |
+----+--------+-----+-------+
限制查询
mysql语句分类关键字为limit,通常位于表名后⾯。
语法:select 字段 from 表名 limit n,m;
limit可以强制指定查询结果的记录条数。
n是开始记录⾏,0表⽰第⼀条记录,m表⽰显⽰⾏,从n开始,共显⽰⼏⾏记录。
时刻注意开始范围时从0开始的,1表⽰的是第⼆⾏,⽽⾮第⼀⾏。
显⽰范围就是共显⽰⼏条记录,并不是结束范围。
例如:查询yunwei表中第2-4⾏记录
mysql>select*from yunwei limit1,3;
+----+--------+-----+-------+
| id | name  | age | sex  |
+----+--------+-----+-------+
|2|张四|21| man  |
|3|张莉|22| woman |
|4|张五|23| man  |
+----+--------+-----+-------+
命令解读:查询yunwei表中记录,从第⼆⾏开始,共显⽰三⾏。
32单片机编程
嵌套查询
没有关键字,嵌套查询分为查询语句和⼦查询语句,在查询语句中含有⼦查询语句,所以叫做嵌套查询。
嵌套⼦查询通常位于查询语句的条件之后。
例如:在caiwu表中查询名称为张三的字段。
先在caiwu表中添加⼀个张三字段,并且年龄不同
mysql>insert into caiwu values(7,'张三',25,'man');
通过⼦查询的⽅式,查询caiwu和yunwei表中名称字段相同的字段
mysql>select name,age from caiwu where name=(select name  from yunwei where age=20);
+--------+-----+
| name  | age |
better splitter+--------+-----+
|张三|25|
+--------+-----+
前半段是正常的查询语句,name=()中为⼦查询语句,查询的字段为name,查yunwei表中age为20的name,结果为张三,将结果执⾏外部的查询语句,就完成了。
使⽤嵌套查询,两个表必须要有⼀个关联字段,否则⽆法实现。
测试完成后,将添加的字段删除即可。
多表连查
多表连查全称多表连接查询,和嵌套⼦查询⼀样,都需要有⼀个共同的字段,然后将多个表连接在⼀起查询,将符合条件的组成⼀个合集。
linux自定义命令常见连接:内连接,外连接
内连接
根据两个表中共有的字段进⾏匹配,然后将符合条件的合集进⾏拼接。
关键字为inner join…on…,通常位于表名后⾯。
语法:select 字段 from 表1 inner join 表2 on 表1.字段=表2.字段;
on后⾯的是连接条件,也就是表1和表2共有的字段
例如:将yunwei和caiwu表连接在⼀起

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。