關於exists和update的語法問題
發發 2014-08-27 02:19:26 有兩個表aa,bb:
lhdata@KKTEST1> select * from aa;
ID NUM
-------- --------
3 6
4 8
2 5
lhdata@KKTEST1> select * from bb;
ID
------
3
4
5
當我跑下面兩句帶exists的搜索:
lhdata@KKTEST1> select * from aa where exists (select 1 from bb where aa.id=bb.id);
ID NUM
-------- --------
3 6
4 8
lhdata@KKTEST1> select * from aa where exists (select 1 from aa,bb where aa.id=bb.id);
ID NUM
-------- --------
3 6
4 8
2 5
我的疑問是上面兩句select的差別在於哪裡?我的目的在於搜索出aa表中aa.id=bb.id的行,為什麼第二句會把不符合條件的行也搜索出來?
同樣地有另外一個關於update的問題(同樣引用上述aa,bb表):
lhdata@KKTEST1> update aa set num = (SELECT aa.id+bb.id num FROM bb WHERE bb.id = aa.id);
3 rows updated.
lhdata@KKTEST1> select * from aa;
ID NUM
-------- --------
3 6
4 8
2
我目的是要把aa表中aa.id=bb.id的行的num列update成aa.id+bb.id,為什麼出來的結果不符合條件的行的num值都update成空了呢?
望大神指點一二。