? ?开发程序1年多了,这您肯定有点思路了,终于开始有些思路了。这是真正进步的开始.real 从2-3.5台阶的进步! 从有点感觉到渐入佳境过渡中.
代码是把现实转化成代码的能力。?
?
--Demo 小区表
create table xiaoqu01(
ID int null,
Xname varchar(100) null,
address varchar(100) null,
Lid int not null, --楼号ID
Yid int null,
);
go
select * from xiaoqu01;
--add data
insert into xiaoqu01(id,lid,yid,xname,address)values(1,1,1,'世茂天城','天桥区');
insert into xiaoqu01(id,lid,yid,xname,address)values(2,2,2,'荣盛首府','历城区');
insert into xiaoqu01(id,lid,yid,xname,address)values(3,3,3,'中海凯旋','历城区');
insert into xiaoqu01(id,lid,yid,xname,address)values(4,1,4,'世茂天城','天桥区');
--重复数据
delete from xiaoqu01 where xname='世茂天城';
--楼号表 louhao Lid
create table louhao(
ID int null,
Lnumber int null, --代表几号楼. 1234
);
go
--查询楼号信息
select * from louhao;
--add data
insert into louhao(id,lnumber)values(1,1);
insert into louhao(id,lnumber)values(2,2);
insert into louhao(id,lnumber)values(3,3);
insert into louhao(id,lnumber)values(2,4);
insert into louhao(id,lnumber)values(2,5);
insert into louhao(id,lnumber)values(3,6);
insert into louhao(id,lnumber)values(4,1);--如花id and 楼号为1
--删除数据
delete from louhao where lnumber=1;
--条件删除
delete from louhao
where id>=2 and Lnumber>=2;
--
delete from louhao where id=1 and Lnumber=4;
--业主表 Yid. 业主信息
create table yezhu(
ID int null,
Yname varchar(100) null,
age int null,
gender bit null,--
marriage varchar(100) null,--
Yearmoney decimal(18,1) null,
);
go
--查询业主表信息
select * from yezhu;
--add data
insert into yezhu(id,yname,age,gender,marriage,Yearmoney)values(1,'张三家',33,'true','未婚',200000);
insert into yezhu(id,yname,age,gender,marriage,Yearmoney)values(2,'李四家',43,'true','已婚',250000);
insert into yezhu(id,yname,age,gender,marriage,Yearmoney)values(3,'王五家',53,'true','已婚',200000);
insert into yezhu(id,yname,age,gender,marriage,Yearmoney)values(1,'如花',50,'FALSE','未婚',20000);
--删除
delete from yezhu where yname='如花';
delete from yezhu where id=1 and yname='如花';
--业主表为主表,在上.楼号表为从表,在下
--
--01 查询每个业主的楼号是多少
select * from xiaoqu01
left join yezhu
on xiaoqu01.yid=yezhu.id --获取到全部业主信息
left join louhao
on xiaoqu01.lid=louhao.id
--02 查询1号楼有哪些业主
select * from xiaoqu01
left join louhao
on xiaoqu01.lid=louhao.id --获取全部楼号的信息
left join yezhu
on xiaoqu01.yid=yezhu.id
-- 1号楼对于楼号表中的ID是什么. 关键是楼号对应的字段,lnumber对应的是谁,条件查询写这里就行了。
where louhao.Lnumber=1;
--03
select * from xiaoqu01
left join yezhu
on xiaoqu01.yid=yezhu.id
left join louhao
on xiaoqu01.lid=louhao.id
-- 如花在业主表中对应的名字等于 如花的姓名
--where yezhu.ID=1; 这里多了一个楼号,然后再加上名字的条件,就可以查询到如花的楼号信息了.
where yezhu.ID=1 and Yname='如花';
--3.1 查询如花住在哪个楼号里面
select * from xiaoqu01
left join yezhu
on xiaoqu01.yid=yezhu.id
left join louhao
on xiaoqu01.lid=louhao.id
-- 如花在业主表中对应的名字等于 如花的姓名
--where yezhu.ID=1; 这里多了一个楼号,然后再加上名字的条件,就可以查询到如花的楼号信息了.
where Lnumber=1 and yezhu.id=4;--yezhu.ID=4 and .01 以这个业主表里面的id,去进行一个区别.
-- 因为同样的1号楼里面,不止一个业主。 区别就是用ID去区别。
效果
?
|