一个有关null的问题?

[复制链接]
查看11 | 回复9 | 2005-10-30 17:05:33 | 显示全部楼层 |阅读模式
select * from emp whereempname=(select empname from emp where empname='zhangni')
and empno in (select empno from emp where empname='test');
例子:
select order_id,product_id,ship_date from line_item
whereproduct_id in (select product_id from line_item where product_id=7489)
and ship_date=(select ship_date from line_item where product_id=7489)
andproduct_id7489;
Which statement about this selectstatement is true?
A. This Select statement produces a nonpairwise comparison of the columns.
B. A candidate row in the LINE_ITEM table must match all the conditions in the Where clause.
C. A condidate row in the LINE_ITEM table must match only one of the conditions in the WHERE clause.
D. The SELECT statement will not return any results if either of the subqueries return a null value.
各位兄弟,答案是A,但是我想知道,那 C 和D 为什么都不对呢?能告诉我理由吗?谢谢!。。。
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
为什么不是B是A呢?match all condition in where clause不对吗?
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
最初由 ahlu 发布
[B]为什么不是B是A呢?match all condition in where clause不对吗? [/B]

兄弟,B我也觉得困惑,但是C和D哪位达人又能告诉我不正确的理由呢?谢谢了。。。
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
c肯定不对呀?match only因该match all
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
最初由 csutzp 发布
[B]select * from emp whereempname=(select empname from emp where empname='zhangni')
and empno in (select empno from emp where empname='test');
例子:
select order_id,product_id,ship_date from line_item
whereproduct_id in (select product_id from line_item where product_id=7489)
and ship_date=(select ship_date from line_item where product_id=7489)
andproduct_id7489;
Which statement about this selectstatement is true?
A. This Select statement produces a nonpairwise comparison of the columns.
B. A candidate row in the LINE_ITEM table must match all the conditions in the Where clause.
C. A condidate row in the LINE_ITEM table must match only one of the conditions in the WHERE clause.
D. The SELECT statement will not return any results if either of the subqueries return a null value.
各位兄弟,答案是A,但是我想知道,那 C 和D 为什么都不对呢?能告诉我理由吗?谢谢!。。。 [/B]

我有以下的見解.,
1. 若這個條件ship_date =(select ship_date from line_item where product_id=7489) 是返回多行數據. 這會令到這句SQL 有問題.
這証明了, 無論你選B,C,D都是錯的. 因為ORACLE會對這句 SQL報錯
我們可以做一個測試.
drop table line_item;
create table line_item
(order_id number, product_id number, ship_date date);
insert into line_item values (1,7489,SYSDATE);
insert into line_item values (2,7489,SYSDATE);
insert into line_item values (3,7489,SYSDATE);
insert into line_item values (4,7490,SYSDATE);
insert into line_item values (5,7491,SYSDATE);
當你咝蓄}目中的SQL. 你會有以下的結果
and ship_date=(select ship_date from line_item where product_id=7489)

*
ERROR at line 4:
ORA-01427: single-row subquery returns more than one row

2. 若ship_date =(select ship_date from line_item where product_id=7489)
這個條件是返回一行數據的., 這句SQL都不會有任何數據返回, 原因是這個條件 product_id in (select product_id from line_item where product_id=7489) 只會返回 7489 的值, 而在整句SQL 中另一個條件product_id7489 去阻止所有PRODUCT_ID是 7489 的數據返回.
我們又來做一個測試
drop table line_item;
create table line_item
(order_id number, product_id number, ship_date date);
insert into line_item values (1,7489,SYSDATE);
當你咝蓄}目中的SQL. 你會有以下的結果
no rows selected
這可以說 B,D 的答案是錯的, 即使所有條件成立或任何一個SUB-QUERY是返回NULL值, 根本沒有任何的candidate row 返回.
3. 對於C 的答案, 好明顯是錯的, 因為WHERE 的句子中全是用AND的條件, 所以這句SQL一定要符合全部條件才會有結果返回.

4, 對於A的答案, 其實可以話題目中最好的答案,
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
发现我的英文水平怎么这么差阿谁能把ABCD都翻译一下 谢谢先
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
tomming兄:
首先感谢你的帮助!
但是,请恕兄弟愚昧,我始终认为D好像也是正确的,因为假设那个只能返回one row的条件确实只返回了1 row,那么其他条件只要有一个为null,那么where的三个条件分别and的结果是不是也是null?
那如果这样的话,D应该也是ok的啊,如何算错呢?不明...
谢谢...
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
若你假设那个只能返回one row的条件确实只返回了1 row, 這樣, A 和 D 都算是正確.
select product_id from line_item where product_id=7489
但product_id 在 line_item 上好明顯不是PRIMARY KEY, 即是有一個可
能性就是有重覆的數據出現的機會, 所以從這個角度來看, 即是 D 有可能是錯的, 但對於A來說, 無論數據是怎樣, 都不會影響 A 的答案.
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
请问楼上兄弟a nonpairwise comparison of the columns.怎么翻译啊!!
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
column 不是一對對地作比較
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行