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

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

普通查询

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。

    通过云服务器的公网IP和端口,使用MySQL客户端工具连接到本地MySQL。 云服务器如何连接本地mysql 随着云计算技术的发展,越来越多的企业和个人选择将数据和应用部署在云服务器上,而在开发和测试过程中,我们经常需…

    2024年7月10日
    02
  • 经验分享jdbc怎么连接mysql数据库。

    要使用JDBC连接MySQL数据库,您需要下载并安装MySQL的JDBC驱动程序。您可以使用Java代码和JDBC驱动类连接到MySQL数据库。以下是一些步骤:,,1. 下载MySQL JDBC驱动程序。,2. 将JDBC驱动程序添加到Java项目的类路…

    2024年7月14日
    06
  • 今日分享mysql保留小数点后两位。

    MySQL保留小数 在MySQL中,我们可以使用不同的数据类型来存储小数,以下是一些常用的数据类型以及它们的取值范围: 1. FLOAT:单精度浮点数,取值范围为-3.4E+38到3.4E+38之间。 2. DOUBLE:双精度浮点数,取值范围为…

    2024年6月18日
    02
  • 关于mysql时间戳是什么意思。

    MySQL时间戳是一种数据类型,用于存储日期和时间信息。有两种形式:1、TIMESTAMP类型,占用4个字节的存储空间,存储的值会根据时区进行转换,存储为UTC,时间戳的值是可以自动更新的;2、DATETIME类型,占用8个字节…

    2024年7月27日
    01
  • 教你mysql存储地图坐标。

    随着地理信息系统(GIS)的发展,三维坐标数据在各个领域的应用越来越广泛,MySQL作为一种关系型数据库管理系统,具有高性能、高可靠性和易扩展性等优点,已经成为许多企业和开发者的首选数据库,本文将对MySQL中三…

    2024年6月20日
    01
  • 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自学教程
    0268
  • 教你云服务器数据库使用的方法是什么意思。

    云服务器是一种基于互联网的计算资源共享模式,它可以提供可扩展、高可用、高性能的计算服务。云服务器上的数据库是指在云服务器上运行的数据库,可以通过网络访问。使用云服务器上的数据库,可以实现数据的备份、…

    2024年7月13日
    02
  • 示例php+mysql查询实现无限下级分类树输出

    本文实例讲述了php+mysql查询实现无限下级分类树输出。分享给大家供大家参考,具体如下:这里介绍的php结合mysql查询无限下级树输出,其实就是无限分类。给各位整理了几个php无限分类的例子.【

    2022年6月17日
    0182

联系我们

QQ:951076433

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