MySQL[练习|面试题]-我的租房网
1 # 创建数据库
2 CREATE DATABASE Tenement;
3
4 # 切换数据库
5 USE Tenement;
6 # 创建客户信息表 sys_user
7 CREATE TABLE sys_user(
8 uid INT PRIMARY KEY AUTO_INCREMENT,
9 uName VARCHAR(20) NOT NULL,
10 uPassWord VARCHAR(20)
11 );
12 DESC sys_user;
13
14 # 创建区县信息表
15 CREATE TABLE IF NOT EXISTS hos_district(
16 did INT PRIMARY KEY AUTO_INCREMENT,
17 dName VARCHAR(50) NOT NULL
18 );
19 DESC hos_district;
20
21 # 创建街道信息表
22 CREATE TABLE IF NOT EXISTS hos_street(
23 sid INT PRIMARY KEY AUTO_INCREMENT,
24 sName VARCHAR(50) NOT NULL,
25 sDid INT NOT NULL
26 );
27 DESC hos_street;
28 # 为sDid 字段添加外键
29 ALTER TABLE hos_street ADD CONSTRAINT
30 FOREIGN KEY(sDid)
31 REFERENCES hos_district(did);
32
33 # -----创建房屋类型表------
34 CREATE TABLE IF NOT EXISTS hos_type(
35 hTid INT PRIMARY KEY AUTO_INCREMENT,
36 htName VARCHAR(20) NOT NULL
37 );
38 DESC hos_type;
39
40 # ------创建出租房信息表--------
41 CREATE TABLE IF NOT EXISTS hos_house(
42 hMid INT PRIMARY KEY AUTO_INCREMENT,
43 uid INT NOT NULL,
44 sid INT NOT NULL,
45 hTid INT NOT NULL,
46 price DECIMAL DEFAULT 0 NOT NULL,
47 topic VARCHAR(50) NOT NULL,
48 contents VARCHAR(255) NOT NULL
49 );
50 ALTER TABLE hos_house ADD hTime DATETIME ;
51 ALTER TABLE hos_house ADD copy VARCHAR(50);
52
53 DESC hos_house;
54 # 为hos_house表添加主外键
55 ALTER TABLE hos_house ADD CONSTRAINT
56 FOREIGN KEY (uid)
57 REFERENCES sys_user(uid);
58
59 ALTER TABLE hos_house ADD CONSTRAINT
60 FOREIGN KEY(sid)
61 REFERENCES hos_street(sid);
62
63 ALTER TABLE hos_house ADD CONSTRAINT
64 FOREIGN KEY(hTid)
65 REFERENCES hos_type(hTid);
建库建表
1 /*
2 *客户信息表
3 */
4
5 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('张三','s217701');
6 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('李四','s217702');
7 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('王鑫','s217703');
8 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('张建','s217704');
9 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('李剑','s217705');
10 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('蒋以然','s217706');
11 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('王晓超','s217707');
12 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('张冬雪','s217708');
13 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('孙鹏','s217709');
14 INSERT INTO sys_user(UNAME,UPASSWORD) VALUES('蒋连昌','s217710');
15 /*
16 *区县信息
17 */
18
19 INSERT INTO hos_district (DNAME) VALUES('海淀区');
20 INSERT INTO hos_district (DNAME) VALUES('西城区');
21 INSERT INTO hos_district (DNAME) VALUES('东城区');
22 INSERT INTO hos_district (DNAME) VALUES('朝阳区');
23 INSERT INTO hos_district (DNAME) VALUES('景山区');
24 INSERT INTO hos_district (DNAME) VALUES('宣武区');
25 INSERT INTO hos_district (DNAME) VALUES('大兴');
26 INSERT INTO hos_district (DNAME) VALUES('丰台');
27 /*
28 *街道信息表
29 */
30
31 INSERT INTO hos_street (SNAME,SDID) VALUES('中关村',1);
32 INSERT INTO hos_street (SNAME,SDID) VALUES('苏州街',1);
33 INSERT INTO hos_street (SNAME,SDID) VALUES('万泉庄',1);
34 INSERT INTO hos_street (SNAME,SDID) VALUES('东四',3);
35 INSERT INTO hos_street (SNAME,SDID) VALUES('东单',3);
36 INSERT INTO hos_street (SNAME,SDID) VALUES('西四',2);
37 INSERT INTO hos_street (SNAME,SDID) VALUES('西单',2);
38 INSERT INTO hos_street (SNAME,SDID) VALUES('东湖',4);
39 INSERT INTO hos_street (SNAME,SDID) VALUES('八里庄',4);
40 INSERT INTO hos_street (SNAME,SDID) VALUES('双井',5);
41 INSERT INTO hos_street (SNAME,SDID) VALUES('陶然亭',5);
42 INSERT INTO hos_street (SNAME,SDID) VALUES('南菜园',6);
43 INSERT INTO hos_street (SNAME,SDID) VALUES('兴丰街',7);
44 INSERT INTO hos_street (SNAME,SDID) VALUES('黄村',7);
45 INSERT INTO hos_street (SNAME,SDID) VALUES('南苑街',8);
46 INSERT INTO hos_street (SNAME,SDID) VALUES('东铁营',8);
47 /*
48 *房屋类型表
49 */
50
51 INSERT INTO hos_type (HTNAME) VALUES('一室一卫');
52 INSERT INTO hos_type (HTNAME) VALUES('一室一厅');
53 INSERT INTO hos_type (HTNAME) VALUES('两室一卫');
54 INSERT INTO hos_type (HTNAME) VALUES('两室一厅');
55 INSERT INTO hos_type (HTNAME) VALUES('三室一厅');
56 INSERT INTO hos_type (HTNAME) VALUES('三室两厅');
57 /*
58 *出租房屋信息表
59 */
60
61 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
62 VALUES(1,1,2,2600,'中关村','中关村一条街','2009-1-2','中关村');
63 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
64 VALUES(2,2,3,3600,'苏州街','苏州街一条街','2009-1-3','苏州街');
65 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
66 VALUES(3,3,4,4600,'万泉庄','万泉庄一条街','2009-1-4','万泉庄');
67 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
68 VALUES(1,3,2,1500,'万泉庄附近','万泉庄附近一条街','2009-7-2','万泉庄附近');
69 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
70 VALUES(1,5,2,2700,'东单','东单很多美食','2009-9-2','东单');
71 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
72 VALUES(3,1,2,2600,'中关村','中关村电脑城','2009-4-1','中关村');
73 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
74 VALUES(4,4,1,2000,'东四','东四一条街','2009-4-2','东四');
75 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
76 VALUES(5,6,3,3600,'西四','西四一条街','2009-1-2','西四');
77 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
78 VALUES(5,7,2,3600,'西单','西单购物城','2009-4-2','西单');
79 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
80 VALUES(6,2,2,2600,'苏州街','苏州街美食','2009-2-2','苏州街');
81 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
82 VALUES(7,8,3,2900,'朝阳东湖','朝阳东湖一景','2009-3-2','朝阳');
83 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
84 VALUES(8,3,1,700,'万泉庄','万泉庄一条街','2009-5-2','万泉庄');
85 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
86 VALUES(3,2,3,4200,'苏州街','苏州街二条街','2009-1-3','苏州街');
87 INSERT INTO hos_house(UID,SID,HTID,PRICE,TOPIC,CONTENTS,HTIME,COPY)
88 VALUES(4,2,3,4100,'苏州街','苏州街西街','2009-1-3','苏州街');
插入数据
1 # 上机4 分页显示查询出租房屋信息 并用 临时表存放
2 CREATE TABLE hos_houseTemp
3 SELECT * FROM hos_house
4 LIMIT 5,5;
5
6 SELECT * FROM hos_houseTemp;
7
8
9
10 # 上机5 查询张三所发布的房屋信息,显示房屋分布的街道和区县
11 SELECT d.`dName` AS 区县,s.`sName` AS 街道,h.htid AS 户型,
12 h.`price` AS 价格,h.`topic` AS 标题,h.`contents` AS 内容,h.`hTime` AS 时间,h.copy AS 备注
13 FROM hos_house h
14 INNER JOIN hos_street s ON s.`sid`=h.`sid`
15 INNER JOIN hos_district d ON d.`did`=s.`sDid`
16 WHERE h.uid = (
17 SELECT uid
18 FROM sys_user WHERE uName='张三'
19 );
20
21 # 另一种做法
22 SELECT *
23 FROM hos_house WHERE uid = (
24 SELECT uid FROM sys_user
25 WHERE uname='张三'
26 )
27 # 子查询前置 生成一个查询结果表 然后再进行多表查询
28 # 这样优化查询速度
29
30 SELECT d.`dName` AS 区县,s.`sName` AS 街道,h.htid AS 户型,
31 h.`price` AS 价格,h.`topic` AS 标题,h.`contents` AS 内容,h.`hTime` AS 时间,h.copy AS 备注
32 FROM (
33 SELECT *
34 FROM hos_house WHERE uid = (
35 SELECT uid FROM sys_user
36 WHERE uname='张三'
37 )
38 ) AS h INNER JOIN hos_street s ON h.sid = s.sid
39 INNER JOIN hos_district d ON s.sdid = d.did
40
41
42 # 上机6 根据户型和房屋所在区县和街道,为至少有两个街道有出租房屋的区县制作出租房屋清单
43 SELECT t.htName AS 户型,u.uName AS 姓名,d.dName AS 区县,s.sName AS 街道名
44 FROM hos_house h
45 INNER JOIN hos_type t ON h.hTid=t.hTid
46 INNER JOIN sys_user u ON h.uid = u.uid
47 INNER JOIN hos_street s ON h.sid=s.sid
48 INNER JOIN hos_district d ON s.sDid = d.did
49 WHERE d.did IN (
50 SELECT d.did
51 FROM hos_house h INNER JOIN hos_street s ON h.sid=s.sid
52 INNER JOIN hos_district d ON s.sDid = d.did
53 GROUP BY d.did
54 HAVING COUNT(DISTINCT s.sid)>1
55 )
56 ORDER BY d.dName
57
58
59 # 上机7 房屋出租统计合并
60 SELECT 1 AS 季度,'合计' AS 区县,' ' AS 街道 ,' ' AS 户型,COUNT(*) AS 房屋数量
61 FROM hos_house h
62 WHERE MONTH(htime)>=1 AND MONTH(htime)<=3
63 UNION
64 SELECT 1 AS 季度,d.dName AS 区县,' ' AS 街道 ,' ' AS 户型,COUNT(*) AS 房屋数量
65 FROM hos_house h INNER JOIN hos_street s ON h.sid=s.sid
66 INNER JOIN hos_district d ON s.sdid=d.did
67 WHERE MONTH(htime)>=1 AND MONTH(htime)<=3
68 GROUP BY d.did
69 UNION
70 SELECT 1 AS 季度,d.dName AS 区县,s.sname AS 街道 ,t.htName AS 户型,COUNT(*) AS 房屋数量
71 FROM hos_house h INNER JOIN hos_street s ON h.sid=s.sid
72 INNER JOIN hos_district d ON s.sdid=d.did
73 INNER JOIN hos_type t ON h.htid=t.htid
74 WHERE MONTH(htime)>=1 AND MONTH(htime)<=3
75 GROUP BY d.did,s.sid,t.htid
76
77 #优化 不能一个季度一个季度的合并
78 # 直接使用QUARTER()季度函数
79 SELECT QUARTER(htime) AS 季度,'合计' AS 区县,' ' AS 街道 ,' ' AS 户型, COUNT(*) AS 房屋数量
80 FROM hos_house
81 GROUP BY QUARTER(htime)
82 UNION
83 SELECT QUARTER(htime) AS 季度,d.dName AS 区县,' ' AS 街道 ,' ' AS 户型, COUNT(*) AS 房屋数量
84 FROM hos_house h,hos_street s,hos_district d
85 WHERE h.sid =s.sid AND s.sdid=d.did
86 GROUP BY QUARTER(htime),d.dname
87 UNION
88 SELECT QUARTER(htime) AS 季度,d.dName AS 区县,s.sname AS 街道 ,t.htName AS 户型, COUNT(*) AS 房屋数量
89 FROM hos_house h,hos_street s,hos_district d,hos_type t
90 WHERE h.sid =s.sid AND s.sdid=d.did AND h.htid = t.htid
91 GROUP BY QUARTER(htime),d.dname,s.sname
92 ORDER BY 季度,区县,街道
MySQL[练习|面试题]-我的租房网的更多相关文章
- Linux运维必会的MySQL企业面试题大全
(1)基础笔试命令考察 1.开启MySQL服务 /etc/init.d/mysqld start service mysqld start systemctl start mysqld 2.检测端口是 ...
- crawler4j源码学习(2):Ziroom租房网房源信息采集爬虫
crawler4j是用Java实现的开源网络爬虫.提供了简单易用的接口,可以在几分钟内创建一个多线程网络爬虫.下面实例结合jsoup解析网页,javacsv存储采集数据:采集自如ziroom租房网(h ...
- (转)linux运维必会MySQL企业面试题
linux运维必会MySQL企业面试题 老男孩教育运维班全体学员MySQL必会企业面试题最实战.最细致.最落地的运维实训基地,老男孩教育连续多年国内平均就业工资最高! 数据库的重要性是所有技术里最核 ...
- (转)Linux运维MySQL必会面试题100道
老男孩教育Linux运维班MySQL必会面试题100道 (1)基础笔试命令考察 (要求:每两个同学一组,一个口头考,一个上机实战作答,每5个题为一组,完成后换位) 1.开启MySQL服务 2.检测端口 ...
- MySQL 基础理论面试题整理
前言: 之前整理公司面试题的时候,看了一篇大神些 SQL 优化之六脉神剑 文章,写的真好! 博主有一些 MySQL 的面试题,简单抽了一个备注一下,补充一下自己不熟悉的地方. 一.在MySQL中, ...
- 数据库MySQL经典面试题之SQL语句
数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...
- 利用requets库采集蘑菇租房网的租房信息
前言:对于我们任何一个漂泊在外的打工者,租房似乎都是我们必经的一个经历,对于我们而言,选择性价比最高,最适合自己的房源至关重要,本文就将利用爬虫技术采集蘑菇租房网上指定的房源信息,后续可以利用这些信息 ...
- mysql面试题及答案,mysql最新面试题,mysql面试题大全汇总
mysql最新面试题及答案汇总 Mysql 的存储引擎,myisam和innodb的区别.mysql最新面试题 答: 1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁, ...
- MySql修改root密码以及允许外网访问
1.修改root密码 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('n ...
随机推荐
- Android Studio IDE 插件开发
作者:字节跳动终端技术--周宸韬 概述 这篇文章旨在向读者介绍IntelliJ IDE插件的开发流程以及常用的一些通用功能,任何基于IntelliJ开发的IDE都可以通过该方式制作插件,例如Andro ...
- python08day
内容回顾 数据类型的补充 str:pass tuple: (1)----->int count 计数 index 通过元组获取索引 list sort 排序从小到大 sort(reverse=T ...
- 【Python爬虫】爬虫利器 requests 库小结
requests库 Requests 是一个 Python 的 HTTP 客户端库. 支持许多 HTTP 特性,可以非常方便地进行网页请求.网页分析和处理网页资源,拥有许多强大的功能. 本文主要介绍 ...
- docker简介及安装(1)
Docker简介 软件开发中最为麻烦的事情可能就是配置环境了.由于用户使用的操作系统具有多样性,即便使用跨平台的开发语言(如Java和Python)都不能保证代码能够在各种平台下都可以正常的运转,而且 ...
- 模拟dom结构
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- uniap tab list 滑动
效果如下 <uni-popup ref="bankListAll" type="dialog"> <ty-mutiple-select :mu ...
- java file已存在,新建流写入此文件是否会覆盖原本file信息
java中建立文件输出流,当文件不存在时会新建一个文件:如果有同名文件,自动覆盖.不存在时自动建立.FileOutputStream的默认构造方法是直接覆盖掉原来的文件,而FileOutputStre ...
- List接口中特有方法
import java.util.ArrayList; import java.util.List; /* List接口中特有方法: 添加 add(int index, E element) addA ...
- [翻译]Introduction to JSON Web Tokens
JWT: Json Web Tokens JWT是一种开放标准(RFC 7519),它定义了一种紧凑且独立的方式,用于将各方之间的信息安全地传输为JSON对象.因为它是经过数字签名的,所以该信息可以进 ...
- 03.python语法入门--注释、变量
注释 """多行注释加在整个py文件的开头用来对该py文件进行一个整体性的说明通常一个py中只有一个多行注释"""# 这是一行贼溜的代码p ...