20200813-mysql数据库查询

20200813-mysql数据库查询

1、删除表数据

(不带where条件的delete)
自增当前值依然从原来基础上进行
(truncate table 表名)
删除全部表,自增值会恢复到初始值重新开始

2、重启数据库服务后,(net stop/start mysql)

ENGINE=INNODB与ENGINE=MYISAM的区别
·对于INNODB的表,自增列从初始值从新开始
·而MYISAM类型的表,自增依然重上一个自增数据基础上开始
INNODB的数据是存在内存里的, MYISAM是存在文件里的
所以重启之后内存里的数据会清空,文件里的数据不会清空

3、DQL数据查询语句(使用频率高,语法较复杂,功能多)

根据用户名和密码在数据库中查询是否存在该用户
核心语法:select * from 表名;
*代表查询所有列(效率低)
#查询指定列(学号 姓名)
// SELECT 列名,列名 from 表名;

#为查询的列或表取别名
// SELECT 列名 别名,列名 别名 from 表名 别名;

#select不会修改原表里面的列名,update修改的是原表的内容
select只会修改呈现给用户的信息。

#使用as,为查询结果取一个新的名字
SELECT CONCAT(‘姓名:’,studentname) AS 新姓名 FROM student;

#查看哪些同学参加了考试(学号)--去除重复,项默认是ALL
//select distinct studentno from result;(所有行都出来了)
·distinct 去掉重复项;

#select查询中可以使用表达式
//select version();
//select 100*3 as 计算结果;

#学员考试成绩集体提升1分
//select studentno,studentresult+1 as 提分后 from student
#满足条件的查询(where) 模糊查询
//select studentno,studentresult
 form result

where studentresult>=95 and studentresult<=100;

#精确查询
//select studentno,studentresult
 form result

where studentno=1000;

#除了1000号同学,我要其他同学的考试成绩
//select studentno,studentresult
 form result

where studentno!=1000;

#模糊查询 between and\ like \ in \ null
 #查询姓李的同学的学号、姓名

#like结合使用的:%(0到任意个字符)_(一个字符)
//select studentno,studentname
from student
where studentname like ‘李%’;

#查询姓李的同学的学号、姓名(名字中只有一个字)
// select studentno,studentname
from student
where studentname like ‘李_’;

#查询姓李的同学的学号、姓名(名字中只有两个字)

// select studentno,studentname
from student
where studentname like ‘李_ _’;

#查询含有'文'的同学的学号、姓名(名字中只有两个字)

// select studentno,studentname
from student
where studentname like ‘%文%’;//应用很多

#查询学员姓名中有% _ 的名字
·需要转义符
// select studentno,studentname
from student
where studentname like ‘%%%’;

// select studentno,studentname
from student
where studentname like ‘%_%’;

// select studentno,studentname
from student
where studentname like ‘%:_%’ escape ‘:’;

#模糊查询和or的功能差不多
//select studentno,studentresult
form result
where in(1000,1002,1003,1004);
#随机产生一个数
//select * from student
where studentno in (select round(rand()*2+1011));

//select studentno,studentresult
form result
where in(‘北京’,‘南京’,‘苏州’,‘扬州’);

#null空

#查询出生日期有(或没有)填写的同学 =null这种写法是错误的
//select studentname
from student
where borndate is (not) null

#区别字符串与null

#查询家庭住址没有写的同学
//select studentname from student
where address =’’ or address is null;

#连接查询
#内连接 inner join :查询两个表中的查询结果的交集
外连接 outer join :
#左右连接 left/right join
·左连接:以左表作为基准,右边表来一一匹配,匹配不上的,
返回左表的记录,右表以null填充
·右连接:以右表作为基准,左边表来一一匹配,匹配不上的,
返回右表的记录,左表以null填充

#自连接
#等值连接 非等值连接

#查询参加了考试的同学信息
select * from student;
select * from result;
#思路:
(1)分析需求,确定插叙的列来源于两个表 student,result,连接查询
(2)确定使用哪一种连接查询?–内连接
#内联
//select student.studentno,studentname,subjectno,subjctresult
form student as s --第一个表
inner join result as r --第二个表
on s.StudentNo=r.StudentNo --两表之间的主外键联系

#左连
//select student.studentno,studentname,subjectno,subjctresult
form student as s --基准表
left join result as r
on s.StudentNo=r.StudentNo
where studentresult is null(查询出右表为空的部分)
[注:where的顺序不能变]
#相当于if else
//case 字段
when 值
then 操作
else操作 end

#右连
//select student.studentno,studentname,subjectno,subjctresult
form student as s
right join result as r --基准表
on s.StudentNo=r.StudentNo

#等值连接 等同与 内连接
//select student.studentno,studentname,subjectno,subjctresult
form student,result
where s.StudentNo=r.StudentNo

#非等值连接 左表m行,右表n行

一共返回m*n行

#union :联合并去重

union all:联合不去重

猜你喜欢

转载自blog.csdn.net/qq_42005540/article/details/107983524