这个视图如何创建!?(详细内容)

woxihuanbohe 2003-07-30 06:47:31
现在有四个表
T1,T2,T3,T4
每个表里面都有一个字段id,T1有自段name,age等
现在创建一个视图把t1.id和t2,t3,t4的id相等的数据放到视图里
Create view v
select t1.id,t1.name,t1.age
from t1,t2,t3,t4
where t1.id=t2.id or t1.id=t3.id or t1.id=t4.id

这样会出现很多数据,怎么回事?大家指点下!
...全文
21 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-07-31
  • 打赏
  • 举报
回复
SELECT img_id, img_length, img_data
FROM dbo.Tab_img
WHERE img_id IN
(SELECT d.id FROM d
union
SELECT h.id FROM h
union
SELECT p.id FROM p
union
SELECT r.id FROM r)
zjcxc 元老 2003-07-31
  • 打赏
  • 举报
回复
SELECT img_id, img_length, img_data
FROM dbo.Tab_img
WHERE img_id IN
(SELECT id from d union select id from h union select id from union select id from r)
愉快的登山者 2003-07-31
  • 打赏
  • 举报
回复
SELECT img_id, img_length, img_data
FROM dbo.Tab_img
WHERE img_id IN
(SELECT d.id FROM d
union SELECT h.id FROM h
union SELECT p.id FROM p
union SELECT r.id FROM r)
woxihuanbohe 2003-07-31
  • 打赏
  • 举报
回复
多谢大家的技术支持,我要的答案是这样子的!


SELECT img_id, img_length, img_data
FROM dbo.Tab_img
WHERE (img_id IN
(SELECT d.id
FROM d, tab_img i
WHERE d.id = i.img_id)) OR
(img_id IN
(SELECT h.id
FROM h, tab_img im
WHERE h.id = im.img_id)) OR
(img_id IN
(SELECT p.id
FROM p, tab_img img
WHERE p.id = img.img_id)) OR
(img_id IN
(SELECT r.id
FROM r, tab_img ii
WHERE r.id = ii.img_id))
woxihuanbohe 2003-07-31
  • 打赏
  • 举报
回复
谢谢我后面的几位前辈指点。我数据库理论没学好,写的代码太长了!

问题已经解决,谢谢各位的支持!
zjcxc 元老 2003-07-30
  • 打赏
  • 举报
回复
即然你是将所有表id相同的记录都查出来,条件就该用and,否则就会形成多对多的关系.当然会出现很多记录了.

Create view v
select t1.id,t1.name,t1.age
from t1,t2,t3,t4
where t1.id=t2.id and t1.id=t3.id and t1.id=t4.id
sdhdy 2003-07-30
  • 打赏
  • 举报
回复
--t1表的3个列,和t2,t3,t4的非id列列出来
Create view vw_test as
select t1.id,t1.name,t1.age,t2.列1,t2.列2,t3.列1,t3.列2,t4.列1,t4.列2
from t1,t2,t3,t4
where t1.id=t2.id and t1.id=t3.id and t1.id=t4.id

yangvxin1 2003-07-30
  • 打赏
  • 举报
回复
Create view v as

select * from t1
where id in(select id from t2 union select id from t3 union select id from t4)
caiyunxia 2003-07-30
  • 打赏
  • 举报
回复
--where t1.id=t2.id and t1.id=t3.id and t1.id=t4.id
txlicenhe 2003-07-30
  • 打赏
  • 举报
回复
Create view v
select t1.id,t1.name,t1.age
from t1,t2,t3,t4
where t1.id=t2.id or t1.id=t3.id or t1.id=t4.id
只要有相等的就查出来

Create view v as
select t1.id,t1.name,t1.age
from t1,t2,t3,t4
where t1.id=t2.id and t1.id=t3.id and t1.id=t4.id
全相等的才查出来
loverforever 2003-07-30
  • 打赏
  • 举报
回复
CREATE VIEW V
AS
SELECT T1.ID,T1.NAME,T1.AGE
FROM T1,T2,T3,T4
WHERE T1.ID=T2.ID AND T1.ID=T3.ID AND T1.ID=T4.ID
pengdali 2003-07-30
  • 打赏
  • 举报
回复
Create view v as
select t1.id,t1.name,t1.age,t2.列,t3.列,t4.列
from t1,t2,t3,t4
where t1.id=t2.id and t1.id=t3.id and t1.id=t4.id

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧