Always think of themselves SQL is very good, did not think of today in the refactoring stored procedure encountered a subquery problem, modified to self-connected after the discovery unexpectedly result is not correct, so have the following test. Suppose the table has the following number of data, ask Query1,query2,query3 query results?
Id |
OrderID |
Transid |
Type |
1 |
1 |
1 |
E |
2 |
1 |
1 |
A |
3 |
1 |
1 |
B |
4 |
1 |
2 |
A |
5 |
1 |
2 |
B |
6 |
2 |
1 |
A |
7 |
2 |
1 |
B |
8 |
2 |
2 |
A |
--Query 1SELECT * fromOrderDetail a Left JoinOrderDetail b onA.orderid=B.orderid andA.transid=B.transid andB.goods='E'WHEREB.goods is NULL--Query 2SELECT * fromOrderDetail aWHEREA.transid<>( SELECT Top 1B.transid fromOrderDetail bWHEREB.orderid=A.orderid andB.goods='E')--Query 3SELECT * fromOrderDetail aWHERE not EXISTS ( SELECTB.transid,b.orderid fromOrderDetail bWHEREB.orderid=A.orderid andB.goods='E')
Please write your answers silently before viewing the results of the operation:
Query1
ID OrderID transid Goods4 1 2 A5 1 2 B6 2 1 A7 2 1 B8 2 2 A
View Code
Query2
ID OrderID transid Goods4 1 2 A5 1 2 B
View Code
Query3
ID OrderID transid Goods6 2 1 A7 2 1 B8 2 2 A
View Code
Test your T-SQL basics