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[练习|面试题]-我的租房网的更多相关文章

  1. Linux运维必会的MySQL企业面试题大全

    (1)基础笔试命令考察 1.开启MySQL服务 /etc/init.d/mysqld start service mysqld start systemctl start mysqld 2.检测端口是 ...

  2. crawler4j源码学习(2):Ziroom租房网房源信息采集爬虫

    crawler4j是用Java实现的开源网络爬虫.提供了简单易用的接口,可以在几分钟内创建一个多线程网络爬虫.下面实例结合jsoup解析网页,javacsv存储采集数据:采集自如ziroom租房网(h ...

  3. (转)linux运维必会MySQL企业面试题

    linux运维必会MySQL企业面试题  老男孩教育运维班全体学员MySQL必会企业面试题最实战.最细致.最落地的运维实训基地,老男孩教育连续多年国内平均就业工资最高! 数据库的重要性是所有技术里最核 ...

  4. (转)Linux运维MySQL必会面试题100道

    老男孩教育Linux运维班MySQL必会面试题100道 (1)基础笔试命令考察 (要求:每两个同学一组,一个口头考,一个上机实战作答,每5个题为一组,完成后换位) 1.开启MySQL服务 2.检测端口 ...

  5. MySQL 基础理论面试题整理

    前言: 之前整理公司面试题的时候,看了一篇大神些 SQL 优化之六脉神剑 文章,写的真好! 博主有一些 MySQL 的面试题,简单抽了一个备注一下,补充一下自己不熟悉的地方.   一.在MySQL中, ...

  6. 数据库MySQL经典面试题之SQL语句

    数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...

  7. 利用requets库采集蘑菇租房网的租房信息

    前言:对于我们任何一个漂泊在外的打工者,租房似乎都是我们必经的一个经历,对于我们而言,选择性价比最高,最适合自己的房源至关重要,本文就将利用爬虫技术采集蘑菇租房网上指定的房源信息,后续可以利用这些信息 ...

  8. mysql面试题及答案,mysql最新面试题,mysql面试题大全汇总

    mysql最新面试题及答案汇总 Mysql 的存储引擎,myisam和innodb的区别.mysql最新面试题 答: 1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁, ...

  9. MySql修改root密码以及允许外网访问

    1.修改root密码 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('n ...

随机推荐

  1. python08day

    内容回顾 数据类型的补充 str:pass tuple: (1)----->int count 计数 index 通过元组获取索引 list sort 排序从小到大 sort(reverse=T ...

  2. Argo 安装和 workflow 实例配置文件解析

    一.Argo 安装配置 1.1 Argo 安装 $ kubectl create ns argo $ kubectl apply -n argo -f https://raw.githubuserco ...

  3. AESUtil_1

    package com.tebon.ams.util;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher; ...

  4. JVM学习二:JVM之GC算法和种类

    我们前面说到了JVM的常用的配置参数,其中就涉及了GC相关的知识,趁热打铁,我们今天就学习下GC的算法有哪些,种类又有哪些,让我们进一步的认识GC这个神奇的东西,帮助我们解决了C 一直挺头疼的内存回收 ...

  5. Ubuntu安装 php + apache + mysql

    转载请注明来源:https://www.cnblogs.com/hookjc/ 1.安装SSH(必须) sudo apt-get install ssh 2.安装MySQL(虽然现在最新版为5.1,但 ...

  6. Mac搭建Git服务器—开启SSH

    SSH开启 在osx中开启ssh访问非常简单,只需要打开"系统偏好设置"并且点击"共享"图标即可. 选中下图中的check box即允许远程登陆.server处 ...

  7. python——虚拟环境管理大合集

    个人常用:pipenv 安装 pip3 install pipenv 创建虚拟环境 # 默认安装在~/.local/virtualenv下 mkdir project cd project pipen ...

  8. 更新docker镜像

    方式一:通过export和import的方式 //导出容器快照到本地文件 $ docker export 1e560fca3906 > ubuntu.tar //将快照文件导入为新的镜像 $ c ...

  9. 4、网络并发编程--僵尸进程、孤儿进程、守护进程、互斥锁、消息队列、IPC机制、生产者消费者模型、线程理论与实操

    昨日内容回顾 操作系统发展史 1.穿孔卡片 CPU利用率极低 2.联机批处理系统 CPU效率有所提升 3.脱机批处理系统 CPU效率极大提升(现代计算机雏形) 多道技术(单核CPU) 串行:多个任务依 ...

  10. Solution -「CF 802C」Heidi and Library (hard)

    \(\mathcal{Descriptoin}\)   Link.   你有一个容量为 \(k\) 的空书架,现在共有 \(n\) 个请求,每个请求给定一本书 \(a_i\).如果你的书架里没有这本书 ...