数据库基础之高级查询(数据库查询)

数据库基础之高级查询(数据库查询)

普通查询

Select 列1[,列2,……] from 表名; 取出对应列所有行的数据;

要项取出对应列几行数据那么要加条件;

select sName,sAge from Student where sAge=18

 

排序

默认情况下查询会按照表中主键升序来显示数据;可以改变

SELECT sid,sName,sAge FROM Student order by sid desc[asc]

默认为升序

多个列之间以逗号分割,优先级依次降低

SELECT sid,sName,sAge FROM Student order by sName,sAge desc

需要条件排序放在条件之后

SELECT sid,sName,sAge FROM Student where sAge>=18 order by sName,sAge desc

 

limit(限制)关键字

limit后面有2个整数关键字,类似于字符串截取,第一个开始记录的索引,第二个代表取的长度

Select * from Student limit 1,1

从第2条记录开始取1条记录

limit关键字优先级:条件>排序>limit

SELECT * FROM `student` where sAge>=18 order by sid desc limit 0,3

 

关键字搜索

查询姓名中包含“张三”所有记录行,

%代表任意个任意的字符,可以是没有

SELECT * FROM `student` WHERE sName like '%张三%'

_代表任意一个字符,不能是没有

SELECT * FROM `student` WHERE sName like '张_'

 

聚合查询

获取数值列的最大max、最小min、平均avg、求和sum、是否重复distinct、统计行数count等查询

Select max(sage),min(sage),avg(sage),sum(sage),distinct(sname),count(sid) from student;

TIPS:聚合查询不得和普通列一起使用,原因是聚合查询一般只有一个值,而普通可能有很多值,非要使用可以分组,普通列必须要放在group by 后!!!

select sname,max(sage) from student 该行错误。

 

分组查询

select sname,max(sage) from student group by sname

 

链接查询

内联

select student.*,sccode from student,studentcode where student.scid=studentcode.scid; (推荐

以上的语法标准的写法

select student.*,sccode from student

inner join

studentcode on student.scid=studentcode.scid

 

左外联

select student.*,sccode from studentcode

left join

student on

student.scid=studentcode.scid

 

右外联

SELECT student . * , sccode

FROM studentcode

RIGHT JOIN student ON student.scid = studentcode.scid

 

子查询

在原来查询的基础继续查询,一般情况下用小括号包含。例如作为临时表

select sid,sname as 姓名,sage,sccode from

(select student.*,studentcode.sccode from student,studentcode where

student.scid=studentcode.scid) as tempTable

还有用子查询作为条件(查询出来的结果只能有一列)

只有一个值

select * from student

where sage=

(select max(sage) from student)

如果有多个值,条件应用in,代表范围

select * from student where sage in

(select sage from student)

还可以有明确条件的写法

select * from student where sage in(19,18)

 

特例

连接查询

Select a+b from table1 –将表table1中查询的a列b列合并输出,也可以2张表合并一列输出

Select a from table1 uinon select b from table2

交叉查询

在链表查询的过程中,有2点需要注意

  • 有重复的列一定要指明属于哪张表
  • 链表条件不对,那么查询出来的数据就是 主键数据条数*外键表数据条件

 

完整的建库建表语句如下

drop database if exists Test;

create database Test default character set utf8 collate

 

utf8_bin;

use Test;

create table StudentCode(

scID int primary key auto_increment,

scCode varchar(18) not null,

unique(scCode)

);

 

create table Student(

sid int primary key auto_increment,

sName varchar(16),

sAge int default 18,

sBalary numeric(6,2) default 0,

scID int not null,

foreign key(scID) references StudentCode(scID),

unique(scID)

);

 

insert into StudentCode values(null,'123');

insert into StudentCode values(null,'456');

insert into StudentCode values(null,'789');

insert into StudentCode(scid) values(null);

insert into StudentCode values(null,'abc');

insert into Student values(null,'张三',default,1,1);

insert into Student values(null,'李四',19,0,2);

insert into Student(sid,sName,scID) values(null,'王五',3);

本文来自投稿,不代表科技代码立场,如若转载,请注明出处https://www.cwhello.com/2935.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
上一篇 2017年10月23日 08:24
下一篇 2017年10月24日 12:04

相关推荐

  • 关于PHP操作数据库的总结

    学的是:PHP操作MySQL 用什么来操作的:PHP的操作MySQL的扩展函数 开启函数库扩展:在php.ini中,php_mysqlxxxx.dll 操作流程: 1.链接数据库 2.选择数据库和设置编码 3.准备SQL语句 4.发送SQL语句 5.接收结果集...

    2018年3月30日
    0373
  • MySQL的基本语法

    注释: 单行注释: #注释内容 单行注释: -- 注释内容(注意,两个“--”之后有一个空格) 多行注释: /*注释内容*/ 语句行: 一条语句也称为一条命令,通常用一个分号(😉结束;也可以通过"delimiter 新结束符" 命...

    2017年11月21日
    0223
  • 数据库的库操作

    库:存储数据的仓库--起名称 表:存储数据的表,一个库可以创建多个表--起名称 字段:给表中的数据起名称 记录:具体一条数据 1.创建数据库 语句:create database 数据库名 库选项; 库选项:字符集、校对集 2.查...

    2017年11月21日 MySQL自学教程
    0175
  • 重蔚自留地php学习第三十五天——mysql基础1

    文件操作:目录操作,文件操作 目录操作步骤: 准备一个目录(路径) 判断一个路径是否是一个目录 打开目录opendir,返回一个目录资源,包含当前目录下所有的文件 遍历目录资源,循环+readdir,每次获得一个文件...

    2018年3月26日 PHP自学教程
    0288
  • ES在MySQL、PHP中的使用

    ES简介一个高扩展、开源的全文检索和分析引擎,它可以准实时地快速存储、搜索、分析海量的数据。全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位...

    2022年6月19日
    0159
  • 重蔚自留地php学习第三十九天——mysql事物触发器函数过程

    数据备份 将数据里的数据进行保存到外部文件,从而在数据库内部数据丢失或者出错的情况下能够通过备份文件进行还原操作,从而将损失降低到最小。 对单表内的纯数据进行备份 将表中的数据(不包含结构,没有字段头...

    2018年10月23日 MySQL自学教程
    0267
  • PHP+Mysql+jQuery实现对当前在线用户数统计方法(附代码)

    我们要统计在一段时间内访问站点的人数,有多种解决方案,你可以使用cookie,session结合文本或者数据库来记录用户访问数。本文将使用PHP,结合Mysql以及jQuery,展示一个统计在线人数以及访客地区分布的示例。 ...

    2018年8月27日
    0195
  • mysql表的字段类型:字符串、日期时间、数值

    值型:存储的数值大小不一样,默认是有符号的,无符号:unsigned 整数:tinyint、smallint、int 小数:float、decimal float,范围大约是-3.4E+38到-1.1E-38、0和1.1E-38到3.4E+38 Decimal:定点型 Decimal(10,2)...

    2017年11月22日 MySQL自学教程
    0260

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息