From 7486208ba325af2d6416f2516e37963315488bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=BB=A4=E9=B9=8F?= <1963831974@qq.com> Date: Mon, 20 Feb 2023 22:47:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230220\344\275\234\344\270\232.md" | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 "12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" "b/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" new file mode 100644 index 0000000..81311d0 --- /dev/null +++ "b/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" @@ -0,0 +1,201 @@ +## 作业 + +#### 第一题 + +```mysql +-- ## 第1题 + +-- 1、创建数据库test01_company +create database test01_company; +use test01_company; +-- 2、创建表格offices +-- +-- | 字段名 | 数据类型 | +-- | ---------- | ----------- | +-- | officeCode | int | +-- | city | varchar(30) | +-- | address | varchar(50) | +-- | country | varchar(50) | +-- | postalCode | varchar(25) | +create table offices( +officeCode int, +city varchar(30), +address varchar(50), +country varchar(50), +postalCode varchar(25) +); +-- 3、创建表格employees +create table employees( +empNum int, +lastName varchar(50), +firstName varchar(50), +mobile varchar(25), +code int, +jioTitle varchar(50), +birth date, +Note varchar(255), +Sex varchar(5) +); +-- | 字段名 | 数据类型 | +-- | --------- | ------------- | +-- | empNum | int(11) | +-- | lastName | varchar(50) | +-- | firstName | varchar(50) | +-- | mobile | varchar(25) | +-- | code | int | +-- | jobTitle | varchar(50) | +-- | birth | date | +-- | Note | varchar(255) | +-- | Sex | varchar(5) | +-- +-- **要求4:**将表employees的mobile字段修改到code字段后面。 +alter table employees modify mobile varchar(25) after code; +-- **要求5:**将表employees的birth字段改名为birthday; +alter table employees change birth birthday date; +-- **要求6:**修改sex字段,数据类型为char(1)。 +alter table employees modify Sex char(1); +-- **要求7:**删除字段note; +alter table employees drop Note; +-- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity varchar(100); +-- **要求9:**将表employees的名称修改为 employees_info +alter table employees rename employees_info; +``` + +### 第二题 + +```mysql +-- ## 第2题 +-- +-- 1、创建数据库test02db +create database test02db; +use test02db; +-- 2、创建表格pet +create table pet( +name varchar(20), +owner varchar(20), +species varchar(20), +sex char(1), +birth year, +death year +); +-- | 字段名 | 字段说明 | 数据类型 | +-- | ------- | -------- | ----------- | +-- | name | 宠物名称 | varchar(20) | +-- | owner | 宠物主人 | varchar(20) | +-- | species | 种类 | varchar(20) | +-- | sex | 性别 | char(1) | +-- | birth | 出生日期 | year | +-- | death | 死亡日期 | year | +-- +-- 3、添加记录 +-- +-- | name | owner | species | sex | birth | death | +-- | ------ | ------ | ------- | ---- | ----- | ----- | +-- | Fluffy | harold | Cat | f | 2003 | 2010 | +-- | Claws | gwen | Cat | m | 2004 | | +-- | Buffy | | Dog | f | 2009 | | +-- | Fang | benny | Dog | m | 2000 | | +-- | bowser | diane | Dog | m | 2003 | 2009 | +-- | Chirpy | | Bird | f | 2008 | | +insert into pet values('Fliffy','harold','Cat','f',2003,2010), +('Claws','gwen','Cat','m',2004,null), +('Buffy',null,'Dog','f',2009,null), +('Fang','benny','Dog','m',2000,null), +('bowser','diane','Dog','m',2003,2009), +('Chirpy',null,'Brid','f',2008,null); +-- 4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth year; +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set owner='kevin' where name='Claws'; +-- 6、 将没有死的狗的主人改为duck +update pet set owner='duck' where species='Dog' and death is null; +-- 7、 查询没有主人的宠物的名字; +select name from pet where owner is null; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select name,owner,death from pet where species='cat' and death is not null; +-- 9、 删除已经死亡的狗 +delete from pet where death is not null and species='dog'; +-- 10、查询所有宠物信息 +select * from pet; +``` + +### 第三题 + +```mysql +# ## 第3题 +# +# 1、创建数据库:test03_company +create database test03_company; +use test03_company; +# 2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +# +# A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +create table department( + depid int primary key not null , + depname varchar(10) not null , + deinfo varchar(50) +); +# B. 雇员表(emoloyee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +create table emoloyee( + empid int primary key not null , + name varchar(10), + sex enum('男','女') default '男' not null , + title varchar(10), + birthday date, + depid int, + foreign key(depid) references department(depid) +); +# * ​ 雇员编号为主键; +# * ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +# * ​ 性别默认为男; +# +# C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +create table salary( + empid int primary key , + basesalary int, + titlesalary int, + deduction int, + foreign key (empid) references emoloyee(empid) +); +# 3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) + +# 4、添加数据如下: +# +# 部门表: +# +# | 部门编号 | 部门名称 | 部门简介 | +# | -------- | -------- | ------------ | +# | 111 | 生产部 | Null | +# | 222 | 销售部 | Null | +# | 333 | 人事部 | 人力资源管理 | +insert into department values (111,'生产部',null), + (222,'销售部',null), + (333,'人事部','人事资源管理'); +# 雇员表: +# +# | 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +# | -------- | ---- | ---- | ---------- | ---------- | ------------ | +# | 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +# | 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +# | 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +# | 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | +insert into emoloyee values (1001,'张三','男','高级工程师','1975-1-1',111), + (1002,'李四','女','助工','1985-1-1',111), + (1003,'王五','男','工程师','1978-11-11',222), + (1004,'张六','男','工程师','1999-1-1',222); +# 工资表: +# +# | 雇员编号 | 基本工资 | 职务工资 | 扣除 | +# | -------- | -------- | -------- | ---- | +# | 1001 | 2200 | 1100 | 200 | +# | 1002 | 1200 | 200 | NULL | +# | 1003 | 2900 | 700 | 200 | +# | 1004 | 1950 | 700 | 150 | +insert into salary values (1001,2200,1100,200), + (1002,1200,200,null), + (1003,2900,700,200), + (1004,1950,700,150); + +``` + -- Gitee From 8a1d036459dd74e56d87c08736047f27aa053104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=BB=A4=E9=B9=8F?= <1963831974@qq.com> Date: Tue, 21 Feb 2023 21:48:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230220\344\275\234\344\270\232.md" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" "b/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" index 81311d0..73aad81 100644 --- "a/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" +++ "b/12 \350\213\217\344\273\244\351\271\217/20230220\344\275\234\344\270\232.md" @@ -199,3 +199,77 @@ insert into salary values (1001,2200,1100,200), ``` +### 第四题 + +```mysql +-- 1、创建一个数据库:test04_school +CREATE DATABASE test04_school CHARSET utf8; +-- 2、创建如下表格 + USE test04_school; + create table Department( + DepNo int(10) COMMENT '部门号' PRIMARY KEY , + DepName varchar(20) COMMENT '部门名称' not null, + DepNote varchar(50) COMMENT '部门备注' null + ); + +-- 表1 Department表的定义 +-- +-- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +-- | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +-- | DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | +-- | DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | +-- | DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | +-- +-- 表2 Teacher表的定义 +CREATE table Teacher( +Number int COMMENT '教工号' PRIMARY KEY, +Name varchar(30) COMMENT '姓名' not null, +Sex varchar(4) COMMENT '性别' null, +Birth date COMMENT '出生日期' null, +DepNo int COMMENT '部门号' , +Salary float COMMENT '工资' null, +Address varchar(100) COMMENT '家庭住址' null, +FOREIGN KEY (DepNo) REFERENCES Department(DepNo) +); + +-- +-- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +-- | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +-- | Number | 教工号 | int | 是 | 否 | 是 | 是 | +-- | Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | +-- | Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | +-- | Birth | 出生日期 | date | 否 | 否 | 否 | 否 | +-- | DepNo | 部门号 | int | 否 | 是 | 否 | 否 | +-- | Salary | 工资 | float | 否 | 否 | 否 | 否 | +-- | Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | +-- +-- 3、添加记录 +INSERT INTO Department VALUES(601,'软件技术系','软件技术等专业'), +(602,'网络技术系','多媒体技术等专业'), +(603,'艺术设计系','广告艺术设计等专业'), +(604,'管理工程系','连锁经营管理等专业'); +-- +-- | **DepNo** | **DepName** | **DepNote** | +-- | --------- | ----------- | ------------------ | +-- | 601 | 软件技术系 | 软件技术等专业 | +-- | 602 | 网络技术系 | 多媒体技术等专业 | +-- | 603 | 艺术设计系 | 广告艺术设计等专业 | +-- | 604 | 管理工程系 | 连锁经营管理等专业 | +INSERT INTO Teacher VALUES(2001,'Tom','女','1970-01-10',602,4500,'四川省绵阳市'), +(2002,'Lucy','男','1983-12-18',601,2500,'北京市昌平区'), +(2003,'Mike','男','1990-06-01',604,1500,'重庆市渝中区'), +(2004,'James','女','1980-10-20',602,3500,'四川省成都市'), +(2005,'Jack','男','1975-05-30',603,1200,'重庆市南岸区'); +-- | **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | +-- | ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | +-- | 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | +-- | 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | +-- | 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | +-- | 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | +-- | 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | +-- +-- 4、用SELECT语句查询Teacher表的所有记录。 +SELECT * FROM Teacher ; + +``` + -- Gitee From bd3471607c18ce8b8bc0a60633d395c0520be11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=BB=A4=E9=B9=8F?= <1963831974@qq.com> Date: Wed, 22 Feb 2023 16:32:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2_\350\277\220\347\256\227\347\254\246.md" | 297 ++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 "12 \350\213\217\344\273\244\351\271\217/20230222\344\275\234\344\270\232\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230222\344\275\234\344\270\232\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" "b/12 \350\213\217\344\273\244\351\271\217/20230222\344\275\234\344\270\232\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000..7cbf120 --- /dev/null +++ "b/12 \350\213\217\344\273\244\351\271\217/20230222\344\275\234\344\270\232\345\237\272\347\241\200\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,297 @@ +## 第1题:员工表 + +```mysql +drop table if exists `employee`; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); +``` + + + +| **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------ | -------- | ------- | ------------ | -------- | ---------- | +| 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | + +**要求1:**查询出薪资在12000~13000之间的员工信息。 + +**要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +**要求3:**将“李四”的家庭住址改为“广东韶关” + +**要求4:**查询出名字中带“小”的员工 + +**要求5:**查询出薪资高于11000的男员工信息 + +**要求6:**查询没有登记电话号码的员工 + +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +**要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```mysql +create database s0222; +use s0222; +drop table if exists `employee`; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); +# **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary>=12000 and salary<=13000; +# **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +select id,name,addr from employee where name like "刘%"; +# **要求3:**将“李四”的家庭住址改为“广东韶关” +update employee set addr='广东韶关' where name='李四'; +# **要求4:**查询出名字中带“小”的员工 +select * from employee where name like "%小%"; +# **要求5:**查询出薪资高于11000的男员工信息 +select * from employee where sex='男' and salary>11000; +# **要求6:**查询没有登记电话号码的员工 +select * from employee where tel is null; +# **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select * from employee where ((addr='广东深圳' and addr='广东广州' ) or salary>12000) and sex='男'; +# **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name 姓名,salary 年薪 from employee; + +``` + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); +``` + +表数据样例: + +```mysql ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| Afghanistan | Asia | 652230 | 25500100 | 20343000000 | +| Albania | Europe | 28748 | 2831741 | 12960000000 | +| Algeria | Africa | 2381741 | 37100000 | 188681000000 | +| Andorra | Europe | 468 | 78115 | 3712000000 | +| Angola | Africa | 1246700 | 20609294 | 100990000000 | ++-------------+-----------+---------+------------+--------------+ +``` + +**要求1:** 查询大国 的国家名称、人口和面积。 + +如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +- 面积至少为 300万平方公里(即,3000000 km2) + +- 人口至少为 2500 万(即 25000000) + +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +**要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 + +**要求4:**查询国家名字中包含“o“字母的国家信息 + +**要求5:**查询GDP值超过10000000000的国家信息 + +**要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +**要求7:**查询人均贡献GDP值低于1000的国家信息。 + +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +```mysql +create database s0222; +use s0222; + +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); +# **要求1:** 查询大国 的国家名称、人口和面积。 +# +# 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +# +# - 面积至少为 300万平方公里(即,3000000 km2) +# +# - 人口至少为 2500 万(即 25000000) +select name,population,area from countries_info where area>=3000000 or population>=25000000; +# **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select * from countries_info where continent='Asia'; +# **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from countries_info where area<10000 and population<100000; +# **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from countries_info where name like "%o%"; +# **要求5:**查询GDP值超过10000000000的国家信息 +select * from countries_info where gdp>10000000000; +# **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select name 国家名,population 人口, gdp DGP值,gdp/population 人均贡献GDP值 from countries_info; +# **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from countries_info where (gdp/countries_info.population)<1000; +# **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name 国家名,area 面积,population 人口,area/population 人均国土面积值 from countries_info; +``` + +## 查询练习第一题 + +```mysql +create database s0222; +use s0222; +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); + + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +# (1)题目:从用户信息表中取出学校的去重数据 +select distinct university from user_profile; +# (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' +select device_id user_infos_example from user_profile; +## (3)题目:查询university是北京大学的设备ID +select device_id from user_profile where university='北京大学'; +## (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 +select id,gender,age,university from user_profile where age>24; +## (5)题目:查询所有用户的设备id、性别、年龄、学校 +select id,gender,age,university from user_profile; +## (6)题目:查询所有用户的数据 +select * from user_profile; +## (7)题目:查询省份是"shanghai"的用户信息 +select * from user_profile where province='Shanghai'; +## (8)题目:查询所有男性用户的设备ID、年龄、学校 +select device_id,age,university from user_profile where gender='male'; +## (9)题目:从用户信息表中取出省份的去重数据 +select distinct province from user_profile; + +``` + +## 运算符练习第二题 + +```mysql +create database s0222; +use s0222; +# 第1题:user_profile表脚本1 +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +# (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 +select device_id,gender,age from user_profile where age>=20 and age<=23; +# (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 +select device_id,gender,age,university from user_profile where university!='复旦大学'; +# (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 +select device_id,gender,age,university from user_profile where age is not null; + +``` + +## 运算符练习第三题 + +```mysql +create database s0222; +use s0222; +# 第2题:user_profile表脚本2 +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`id` int , +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`gpa` float, +`active_days_within_30` float, +`question_cnt` float, +`answer_cnt` float +); + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); +INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3,15,7,13); +# (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa +select id,device_id,gender,age,university,gpa from user_profile where gpa>3.5 and gender='male'; +# (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile where university='北京大学' or gpa>3.7; +# (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile where university='北京大学'or university='复旦大学'or university='山东大学'; +# (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 +select * from user_profile where (gpa>3.5 and university='山东大学') or (gpa>3.8 and university='复旦大学'); +# (8)题目:所有大学中带有北京的用户信息 +select * from user_profile where university like "%北京%"; +``` + -- Gitee