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. windows下使用LTP分词,安装pyltp

    1.LTP介绍 ltp是哈工大出品的自然语言处理工具箱, 提供包括中文分词.词性标注.命名实体识别.依存句法分析.语义角色标注等丰富. 高效.精准的自然语言处理技术.pyltp是python下对ltp ...

  2. python15day

    昨日回顾 装饰器:完美的呈现了开放封闭原则.本质:闭包. def wrapper(f): def inner(*args,**kwargs): '''在执行被装饰函数之前,想写什么代码写什么代码''' ...

  3. Java多线程专题1: 并发与并行的基础概念

    合集目录 Java多线程专题1: 并发与并行的基础概念 什么是多线程并发和并行? 并发: Concurrency 特指单核可以处理多任务, 这种机制主要实现于操作系统层面, 用于充分利用单CPU的性能 ...

  4. django之djangorestframework序列化操作

    只介绍序列化操作:serializers.ModelSerializer 与 serializers.Serializer 使用序列化的目的:将数据序列化成  JSON 对象 当页面上需要动态加载内容 ...

  5. 关于https域名下的页面iframe嵌套http页面的问题

    业务场景:在一个https域名下用iframe嵌套一个http域名的页面,会直接报错的,报错信息如下: 这段话的意思是:http域名的页面是通过https域名页面加载的,在一个安全的页面不允许加载一个 ...

  6. 浅谈C#可变参数params

    前言 前几天在群里看到群友写了一个基础框架,其中设计到关于同一个词语可以添加多个近义词的一个场景.当时群友的设计是类似字典的设计,直接添加k-v的操作,本人看到后思考了一下觉得使用c#中的params ...

  7. Zookeeper应用场景汇总(超详细)

    Zookeeper典型应用场景汇总 数据发布与订阅(配置中心) 发布与订阅模型,即所谓的配置中心,顾名思义就是发布者将数据发布到ZK节点上,供订阅者动态获取数据,实现配置信息的集中式管理和动态更新.例 ...

  8. UIView属性的讲解

    1.父控件和子控件的理解在storyboard中只有UIView是可以在里面拖入子控件的,其他控件不可以(必须通过代码添加)拖入一个UIView控件,在里面添加一些子控件(UIView控件是控制器的V ...

  9. fckeditor for php 上传图片文件名中文乱码,中文文章乱码

    转载请注明来源:https://www.cnblogs.com/hookjc/ 中文名乱码是因为:FCKeditor使用UTF-8编码,自己机子比如是Windows系统文件名使用的是GBK编码,在上传 ...

  10. js修改css

    转载请注明来源:https://www.cnblogs.com/hookjc/ <style type="text/css"> .style{font-size:9pt ...