sql - Difficulty to understand SQLite exists and its subquery -
assume there 1 table called temp, schema , data listed blow
table temp :
a b ---- ---- 1 1 # row 1 1 5 # row 2 2 2 # row 3
sqlite query :
1) delete temp exists (select 1 temp il2 temp.a = il2.a , temp.b = 1 , il2.b = 5) 2) delete temp exists (select 1 temp il2 temp.a = il2.a , temp.b = 5 , il2.b = 1)
question 1) meaning of "temp.a = il2.a"? il2 alias of temp, compares itself? lost here.
question 2) query 1 , 2 deletes row 1 , 2 experiment, respectively. seems me return value of sub-query "(select 1 temp il2 temp.a = il2.a , temp.b = 5 , il2.b = 1)" controlled value of "il2.b". thought sub-query return both row 2 , row 3.
the delete statements record same a, other b (1 vs. 5) in same table. in order table alias (il2) needed able distinguish between 1 record , other. queries little obfuscated, though. criteria hidden in subquery not belongs. better be:
delete temp b = 1 , exists (select 1 temp il2 il2.a = temp.a , il2.b = 5); delete temp b = 5 , exists (select 1 temp il2 il2.a = temp.a , il2.b = 1);
a side remark: second statement not find (and hence delete) of course, because b1 b5 exists deleted then. in order circumvent , delete both records, i.e. both b1 , b5, combine 2 statements:
delete temp b in (1,5) , exists ( select * temp other other.b in (1,5) , other.a = temp.a , other.b <> temp.b );
Comments
Post a Comment