all_tba_comments & all_tables 连接的奇怪现象,困惑ing

物润声无 2010-12-13 03:14:39

select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%M%' or tab.table_name like'%W%'
and tab.table_name = comm.table_name
and tab.owner = comm.owner
order by owner

-- 在两个表中同一个owner 下 table_name 应该是唯一的吧

-- 例如:select * from all_tab_comments where table_name = 'SRMFORMDETAILS'
-- 只有一条记录,

-- select * from all_tables where table_name = 'SRMFORMDETAILS'
-- 也只有一条记录,


-- 两个表用table name 连接的话,应该也只有一条记录出现,
-- 可是结果却有n 条, 为什么?

--1 CRMUSER SRMFORMDETAILS This table holds all the address ......
--2 CRMUSER SRMFORMDETAILS
--3 CRMUSER SRMFORMDETAILS
--4 CRMUSER SRMFORMDETAILS This table is for internal use. ......
--5 CRMUSER SRMFORMDETAILS
--6 CRMUSER SRMFORMDETAILS
--......



为什么??????
...全文
188 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
minitoy 2010-12-16
  • 打赏
  • 举报
回复
不客气,其实类比成加法和乘法更好理解和记忆.
物润声无 2010-12-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 minitoy 的回复:]
or相当于集合中的并集
and相当于集合中的交集.
肯定是先算交集再算并集啊.
遇到不确定优先等级的运算就加括号吧
[/Quote]
谢谢了
minitoy 2010-12-15
  • 打赏
  • 举报
回复
or相当于集合中的并集
and相当于集合中的交集.
肯定是先算交集再算并集啊.
遇到不确定优先等级的运算就加括号吧
minitoy 2010-12-15
  • 打赏
  • 举报
回复
呵呵,是逻辑运算的优先级问题,[Quote=引用 10 楼 gjswxhb 的回复:]
引用 7 楼 minitoy 的回复:
你原来写的相当于

SQL code
SELECT * FROM
(select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%M%'
UNION ALL
selec……
[/Quote]
物润声无 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 minitoy 的回复:]
你原来写的相当于

SQL code
SELECT * FROM
(select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%M%'
UNION ALL
select tab.owne……
[/Quote]
再问一下: 为什么啊?



select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%MF%'

or

(tab.table_name like '%WMS%'
and tab.table_name = comm.table_name
and tab.owner = comm.owner)
order by owner

-- 这段代码和最开始的那段结果是一样的,
-- 是不是可以按优先级来理解解: and 的优先级比or的要高,
-- oracle 里面没有听说关键字还有优先级啊, 有吗?

-- 觉得还有些问题没相通 想想想想想想想想。。。。
物润声无 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 minitoy 的回复:]
第一部分笛卡尔积了
[/Quote]
谢谢,结贴了。
minitoy 2010-12-14
  • 打赏
  • 举报
回复
第一部分笛卡尔积了
minitoy 2010-12-14
  • 打赏
  • 举报
回复
你原来写的相当于
SELECT * FROM 
(select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%M%'
UNION ALL
select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like'%W%'
and tab.table_name = comm.table_name
and tab.owner = comm.owner)
ORDER BY OWNER;
lxyzxq2008 2010-12-14
  • 打赏
  • 举报
回复

--我这里不会重复的,只有一条啊
select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%YYQ4%'

and tab.table_name = comm.table_name
and tab.owner = comm.owner
----------------------------------
ower TABLE_NAME COMMENTS
CASKOKYOTEST YYQ4
CASKOKYO YYQ4

minitoy 2010-12-14
  • 打赏
  • 举报
回复
select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where (tab.table_name like '%M%' or tab.table_name like'%W%' )
and tab.table_name = comm.table_name
and tab.owner = comm.owner
order by owner
物润声无 2010-12-14
  • 打赏
  • 举报
回复
问什么这几天这么冷清?
物润声无 2010-12-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lxyzxq2008 的回复:]
我这里一跑就死机了,应该是笛卡尔了吧
[/Quote]

问题出在:or tab.table_name like'%W%'


select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%M%'

and tab.table_name = comm.table_name
and tab.owner = comm.owner

union

select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like'%W%'
and tab.table_name = comm.table_name
and tab.owner = comm.owner
order by owner



这个没问题,再想想为什么?
lxyzxq2008 2010-12-13
  • 打赏
  • 举报
回复
我这里一跑就死机了,应该是笛卡尔了吧
物润声无 2010-12-13
  • 打赏
  • 举报
回复
select * from all_tab_comments where table_name = 'SRMFORMDETAILS'
的结果如下,

This table contains the details of each and every field ...

居然和上面的地一条和第4条不一样?


select tab.owner,tab.table_name,comm.comments
from all_tables tab,
all_tab_comments comm
where tab.table_name like '%M%' or tab.table_name like'%W%'
-- 这不是笛卡尔积啊
and tab.table_name = comm.table_name
and tab.owner = comm.owner
order by owner

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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