由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 求问一个关于SQL的exist的问题
相关主题
這個SQL sub-query 是否畫蛇添足 ?Import data in R
SQL combine two tables into one table and add a new column (转载)SQL 有证书考试吗?
SQL find distinct values in large table (转载)用SQL或者ACCESS来做Query design/data extraction
[问题]怎么用proc sql获取row number的值[Mysql] how to return NULL count in group by query
请问SAS大牛一个关于proc sql join请教一道sas 题
请教一个SAS code请大侠指教,在R 里面run query的问题。
[Teradata] How to randomly select one observation from each group?面试的SQL问题一般问些啥?
问一下SQL in Python。啥叫DATABASE STRUCTURE?
相关话题的讨论汇总
话题: nt话题: where话题: select话题: insert话题: values
进入Statistics版参与讨论
1 (共1页)
c*********u
发帖数: 607
1
code如下:
create table nt (x int, y int);
insert into nt values (10, 10);
insert into nt values (10, 20);
insert into nt values (20, 10);
insert into nt values (30, 40);
insert into nt values (30, 50);
insert into nt values (30, 60);
insert into nt values (40, 70);
select * FROM nt WHERE exists
(SELECT t.* FROM nt t WHERE nt.x = t.x AND nt.y > t.y) ;
select * FROM nt WHERE exists
(SELECT nt.* FROM nt t, nt nt WHERE nt.x = t.x AND nt.y > t.y) ;
网上跑SQL的结果的链接在这里:
http://ideone.com/3CtqN9
第一个query的结果是
10|20
30|50
30|60
第二个query的结果是
10|10
10|20
20|10
30|40
30|50
30|60
40|70
我的问题是:
1,我不理解为什么第二个query的结果是全部row都选中?
2,如果用SAS来泡,第一个query是没有结果的,但是如果把data输入两遍,变成nt,t
两个表格,就和ideone的第一个query跑出来的一样了,请问为什么?
非常感谢,回答的人我会尽量发包子的!(看我自己包子有多少。。。)
O***T
发帖数: 124
2
In your second query, the subquery
SELECT nt.* FROM nt t, nt nt WHERE nt.x = t.x AND nt.y > t.y
doesn't have any join with you main query, alias nt only refer to the table
in your subquery.
It can be rewrite to SELECT a.* FROM nt t, nt a WHERE a.x = t.x AND a.y > t.
y
In this way, it is very obvious there is no join between your subquery with
the main one. Exist function here only plays a role like checking if the
statement is correct.
So be careful with the alias. Don't use the same one. It will be quite
confusing...
O***T
发帖数: 124
3
I only know SQL, don't know SAS. So I can only answer your first one.
t*****w
发帖数: 254
4
excellent job!

table
t.
with

【在 O***T 的大作中提到】
: In your second query, the subquery
: SELECT nt.* FROM nt t, nt nt WHERE nt.x = t.x AND nt.y > t.y
: doesn't have any join with you main query, alias nt only refer to the table
: in your subquery.
: It can be rewrite to SELECT a.* FROM nt t, nt a WHERE a.x = t.x AND a.y > t.
: y
: In this way, it is very obvious there is no join between your subquery with
: the main one. Exist function here only plays a role like checking if the
: statement is correct.
: So be careful with the alias. Don't use the same one. It will be quite

1 (共1页)
进入Statistics版参与讨论
相关主题
啥叫DATABASE STRUCTURE?请问SAS大牛一个关于proc sql join
Sas advance chapter quiz 一问请教一个SAS code
请教:SAS table insert into table on MS SQL server[Teradata] How to randomly select one observation from each group?
SQL相关的Interview问题请教问一下SQL in Python。
這個SQL sub-query 是否畫蛇添足 ?Import data in R
SQL combine two tables into one table and add a new column (转载)SQL 有证书考试吗?
SQL find distinct values in large table (转载)用SQL或者ACCESS来做Query design/data extraction
[问题]怎么用proc sql获取row number的值[Mysql] how to return NULL count in group by query
相关话题的讨论汇总
话题: nt话题: where话题: select话题: insert话题: values