最初由 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的答案, 其實可以話題目中最好的答案,
|