MySQL查询语句

MySQL查询语句

select 字段(结果集) from 表名(数据源) [where 条件] [group by 分组][having 条件][order by 排序 asc|desc][limit限制 s,n]
create table stuinfo(
sid int auto_increment primary key comment'学号(主键)',
sname varchar(255) comment'学生名字',
sex enum('男','女') comment'性别',
age tinyint comment'年龄',
city varchar(64) comment'地级市'
)engine=innodb;

create table stumarks(
stuno int primary key comment'学号(主键)',
ch tinyint comment'语文成绩',
math tinyint comment'数学成绩'
)engine=innodb;


insert into stuinfo values(null,'大恒',2,18,'宿州'),(null,'小京',1,20,'北京'),(null,'小强',1,22,'北京'),(null,'小力',1,20,'天津'),(null,'小丽',2,21,'重庆'),(null,'小芳',2,20,'天津');

insert into stumarks values(1,88,99),(2,89,100),(3,67,76),(4,50,59),(5,100,99),(6,96,91);

(1)字段表达式

select 查询也可以用来做计算

select unix_timestamp(); --显示系统时间戳

select rand();--显示随机数

(2)from子句

from后面跟的是什么?是数据源(也就是表)
数据源可以有多张表,返回的是笛卡尔积.

(3)dual表

dual表不是一个实实在在存在的表,它是为了保证select语句的完整性的.

(4)where子句

where子句在数据源进行筛选;

select * from stuinfo where sex=1;

(5)is null|is not null

筛选数据为空或不为空

select * from stumarks where ch is null or math is null;

(6)between| not between

筛选指定范围内的数据
select * from stumarks where ch between 75 and 100;
select * from stumarks where ch>=75 and ch<=100;

(7)运算符

a.算数运算符
+ - * / % ++ --
b.比较运算符
> < >= <=  (<> !=)不等于
c.逻辑运算符
andornot

(8)聚合函数

sum()  #求和
avg()  #求平均值
max()  #最大值
min()  #最小值
count() #统计 

(9)通配符

_  #匹配一个字符

%  #匹配所有

(10)模糊查询(like)

select * from stuinfo where sname like '_明';
select * from stuinfo where sname like '%佰%';

(11)分组查询 group by

将查询的结果分组显示,分组的目的在于方便统计.

select sid,group_concat(sname),sex,age,city from stuinfo group by city;
#每个结果只显示一个

select stuno,ch,math,(ch+math) as score from stumarks group by score;


#group_concat()可以将同一组的字段连接在一起
mysql> select sid,group_concat(sname),sex,age,city from stuinfo group by city;
+-----+---------------------+------+------+------+
| sid | group_concat(sname) | sex  | age  | city |
+-----+---------------------+------+------+------+
|   1 | 小明                   | ?    |   18 | 上海    |
|   2 | 小刚,小强                | ?    |   20 | 北京     |
|   4 | 尹佰力,小芳                  | ?    |   20 | 天津     |
|   5 | 小丽                   | ?    |   21 | 重庆     |
+-----+---------------------+------+------+------+
4 rows in set (0.00 sec)

#安数字分组,会按照升序排列
select * from stuinfo group by age;

#多列分组

select sid,group_concat(sname),sex,age,city from stuinfo group by sex,city;

(12)回溯统计

#在统计的基础上在统计一次
#group_concat()
select sid,group_concat(sname),sex,age,city from stuinfo group by sex,city with rollup;

(13)having条件

where:设置查询的条件,字段必须是存在的
having: 设置查询的条件,条件字段必须在结果集中;
#having  的查询效率要高于where

select * from stuinfo where age>20;   #正确
select * from stuinfo having age>20;  #正确
select sname from stuinfo where age>20;  #正确
select sname from stuinfo having age>20; #错误,原因age不在结果集中(没有查询age这个字段)
select sname,age from stuinfo having age>20; #正确

(14)order by排序[asc|desc]

用书数据排序的

select * from stuinfo order by age asc; #asc 可以不写 ,它是默认值
select * from stuinfo order by age desc;#降序排列

(15)limit(限制)

limit  起始位置,显示长度
select * from stuinfo limit 3; #第一个不写默认从1开始
select * from stuinfo limit 2,3;

(16)查询语句中的选项

all :和 * 是一个意思,表示查询所有的
distinct: 去除重复的数据

select all from stuinfo;
select distinct city from stuinfo;

(17)insert…select…

CREATE TABLE `stuinfoo` (
  `sid` int(11) NOT NULL AUTO_INCREMENT ,
  `sname` varchar(255) DEFAULT NULL,
  `sex` enum('男','女') DEFAULT NULL ,
  `age` tinyint(4) DEFAULT NULL ,
  `city` varchar(64) DEFAULT NULL ,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;

#选择一个表中的数据插入到另一个表中(两个表结构要一致)
insert into stuinfoo select * from stuinfo;

(18)on duplicate key update

作用:在插入数据的时候,如果这个数据已经存在或不满足唯一约束的条件就执行更新;

insert into stuinfo values(2,'小月',2,18,'重庆') on duplicate key update sname='小月',sex=2;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值