首页 > 数据库 > MySQL > 正文

详解MySql基本查询、连接查询、子查询、正则表达查询

2024-07-24 12:45:47
字体:
来源:转载
供稿:网友

查询数据指从数据库中获取所需要的数据。查询数据是数据库操作中最常用,也是最重要的操作。用户可以根据自己对数据的需求,使用不同的查询方式。通过不同的查询方式,可以获得不同的数据。MySQL中是使用SELECT语句来查询数据的。在这一章中将讲解的内容包括。

1、查询语句的基本语法
2、在单表上查询数据
3、使用聚合函数查询数据
4、多表上联合查询
5、子查询
6、合并查询结果
7、为表和字段取别名
8、使用正则表达式查询

什么是查询?

怎么查的?

数据的准备如下:

create table STUDENT( STU_ID int primary KEY, STU_NAME char(10) not null, STU_AGE smallint unsigned not null, STU_SEX char(2) not null ); insert into STUDENT values(2001,'小王',13,'男'); insert into STUDENT values(2002,'明明',12,'男'); insert into STUDENT values(2003,'红红',14,'女'); insert into STUDENT values(2004,'小花',13,'女'); insert into STUDENT values(2005,'天儿',15,'男'); insert into STUDENT values(2006,'阿猎',13,'女'); insert into STUDENT values(2007,'阿猫',16,'男'); insert into STUDENT values(2008,'阿狗',17,'男'); insert into STUDENT values(2009,'黑子',14,'男'); insert into STUDENT values(2010,'小玉',13,'女'); insert into STUDENT values(2011,'头头',13,'女'); insert into STUDENT values(2012,'冰冰',14,'女'); insert into STUDENT values(2013,'美丽',13,'女'); insert into STUDENT values(2014,'神乐',12,'男'); insert into STUDENT values(2015,'天五',13,'男'); insert into STUDENT values(2016,'小三',11,'男'); insert into STUDENT values(2017,'阿张',13,'男'); insert into STUDENT values(2018,'阿杰',13,'男'); insert into STUDENT values(2019,'阿宝',13,'女'); insert into STUDENT values(2020,'大王',14,'男');

然后这是学生成绩表,其中定义了外键约束

create table GRADE( STU_ID INT NOT NULL, STU_SCORE INT, foreign key(STU_ID) references STUDENT(STU_ID) ); insert into GRADE values(2001,90); insert into GRADE values(2002,89); insert into GRADE values(2003,67); insert into GRADE values(2004,78); insert into GRADE values(2005,89); insert into GRADE values(2006,78); insert into GRADE values(2007,99); insert into GRADE values(2008,87); insert into GRADE values(2009,70); insert into GRADE values(2010,71); insert into GRADE values(2011,56); insert into GRADE values(2012,85); insert into GRADE values(2013,65); insert into GRADE values(2014,66); insert into GRADE values(2015,77); insert into GRADE values(2016,79); insert into GRADE values(2017,82); insert into GRADE values(2018,88); insert into GRADE values(2019,NULL); insert into GRADE values(2020,NULL);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表