56,687
社区成员
发帖
与我相关
我的任务
分享
mysql> select * from bcd;
+------+------+-------+----------+----------+-------+-------+-------+
| biao | id | shuzi | xingming | nianling | btese | ctese | dtese |
+------+------+-------+----------+----------+-------+-------+-------+
| b | 1 | 12 | zhangsan | NULL | 99 | NULL | NULL |
| c | 1 | 35 | NULL | 22 | NULL | 55 | NULL |
| d | 1 | 22 | lisi | NULL | NULL | NULL | 66 |
+------+------+-------+----------+----------+-------+-------+-------+
create view bcd
as
select
'b' as biao,id,shuzi,xingming,null as nianling,btese,null as ctese,null as dtese
from b
union all
select
'c' as biao,id,shuzi,null as xingming,nianling,null as btese,ctese,null as dtese
from c
union all
select
'd' as biao,id,shuzhi,xingming,null as nianling,null as btese,null as ctese,dtese
from d;
select
a.id,a.biao,a.riqi,a.bid,a.zhi,bcd.shuzi,bcd.xingming,bcd.nianling,bcd.btese,bcd.ctese,bcd.dtese
from
a,bcd where a.biao=bcd.biao and a.bid=bcd.id;
+------+------+-----------+------+------------+-------+----------+----------+-------+-------+-------+
| id | biao | riqi | bid | zhi | shuzi | xingming | nianling | btese | ctese | dtese |
+------+------+-----------+------+------------+-------+----------+----------+-------+-------+-------+
| 1 | b | 2014/10/1 | 1 | zhangsan12 | 12 | zhangsan | NULL | 99 | NULL | NULL |
| 2 | c | 2014/10/1 | 1 | 57 | 35 | NULL | 22 | NULL | 55 | NULL |
| 3 | d | 2014/10/1 | 1 | 22lisi | 22 | lisi | NULL | NULL | NULL | 66 |
+------+------+-----------+------+------------+-------+----------+----------+-------+-------+-------+
这结构太混乱了,重新设计你的数据库建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。 参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382 1. 你的 create table xxx .. 语句 2. 你的 insert into xxx ... 语句 3. 结果是什么样,(并给以简单的算法描述) 4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL) 这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
select * from a,b,c,d where a.id=b.id and b.id=c.id and c.id=d.id
a表 c表
id biao riqi bid zhi id riqi shuzi nianling ctese
1 b 2014/10/1 1 张三12 1 2014/10/1 35 22 55
2 c 2014/10/1 1 57 d表
3 d 2014/10/1 1 22李四 id riqi shuzhi xingming dtese
b表 1 2014/10/1 22 李四 66
id riqi shuzi xingming btese
1 2014/10/1 12 张三 99
最终视图
id biao riqi bid zhi shuzi xingming nianling btese ctese dtese
1 b 2014/10/1 1 张三12 12 张三 99
2 c 2014/10/1 1 57 35 22 55
3 d 2014/10/1 1 22李四 22 李四 66
不好意思 全乱了
数据库中有四张表 A、B、C、D 其中A表 是BCD三张表的汇总,但是BCD表中有还有各自的一些信息没有汇总到A表中,现在我想做一个视图,把A、B、C、D中的数据汇总显示到一个视图里面。我用A表和B表创建视图的时候数据正常,但是用A表和B表和C表三个表创建视图的时候,出来的数据就混乱了,这个该怎么做呢?