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

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

普通查询

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

相关推荐

  • mysql_建立索引的优缺点

    建立索引的优缺点: 为什么要创建索引呢? 这是因为,创建索引可以大大提高系统的性能。  第一、通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。 第二、可以大大加快 数据的检索速度,这也是创建索引...

    2017年10月19日
    0173
  • PHP中如何进行全文检索?

    随着互联网技术的不断发展,数据量的爆炸式增长和各种文本数据的广泛应用,全文检索成为了一项非常重要的技术。全文检索是一种能够快速、准确地查找文本数据的方法,广泛应用于搜索引擎、论坛、博客、电商网站等...

    2023年5月17日
    02
  • 重蔚自留地php学习第三十九天——关于mysql事物触发器函数过程的总结

    数据备份 对数据表内容进行备份 备份:select [*/字段列表] into outfile 文件路径 [字段处理] [行处理] from 表 还原:load data infile 文件路径 into 表 [字段处理] [行处理] 备份 ---------------》 数据库 -...

    2018年10月26日
    0239
  • 重蔚自留地php学习第三十五天——mysql基础1

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

    2018年3月26日 PHP自学教程
    0288
  • MySQL数据库性能优化之一(缓存参数优化)

    数据库属于 IO密集型的应用程序,其主要职责就是数据的管理及存储工作。而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级。所以,要优化数据...

    2017年12月26日
    0178
  • mysql的数据操作_删除数据

    语句:delete from 表名 where条件 [order排序] [limit限定]; Where条件必须添加,否则删除所有数据,建议id,原因id唯一性 删除大范围的数据。 Order:当前表倒序还是正序,不用添加,默认就可以了 Limit:限定...

    2017年11月24日
    0200
  • MySQL中如何定位慢查询?

    做压测的时候有的接口非常的慢,接口的响应时间超过了2秒以上,在运维的监控系统Skvwalking中,在展示的报表中可以看到是哪一个接口比较慢,分析这个接口,可以看到哪部分比较慢,从而知道SQL的具体的执行时间,...

    2023年8月29日
    00
  • PHP操作MySQL的流程

    1.链接数据库 2.选择数据库并设置编码 3.准备SQL语句 4.发生SQL语句到MySQL服务器 5.接收返回的结果集资源 6.解析结果集资源 7.关闭链接资源 流程图:

    2018年3月23日
    0290

联系我们

QQ:951076433

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