if you need a subquery to return multiple columns for comparison purpose then its allowed so you can do something like
but if you want something like
then mysql wont allow you to select 2 columns in the first subquery.
To avoid this you can turn the query into a join query and write a wrapper query on top like
but I ran into an issue on weekend where table1 had 1.4M rows and table 2 had 40K rows and table3 had 3K rows. Now joining 1.4M to 44K rows was not coming out of Mysql database even in 10 minutes so this approach was out of question. The final result set size was 44K.
So finally the best solution I could find on weekend to fix this to concatenate columns and split them in java code.
select x from table1 where (a,b)= (select a,b from table2)
but if you want something like
select (select a,b from table1 where table1.x=table2.x) fields, table2.y, table2.z from table2 join table3 on table2.aa=table3.bb where table2.xx=10
then mysql wont allow you to select 2 columns in the first subquery.
To avoid this you can turn the query into a join query and write a wrapper query on top like
select a,b,y,z from (table1 join table2 on table1.x =table2.x) join table3 on table2.aa=table3.bb where table2.xx=10
but I ran into an issue on weekend where table1 had 1.4M rows and table 2 had 40K rows and table3 had 3K rows. Now joining 1.4M to 44K rows was not coming out of Mysql database even in 10 minutes so this approach was out of question. The final result set size was 44K.
So finally the best solution I could find on weekend to fix this to concatenate columns and split them in java code.
select (select concat(a,'|',b) from table1 where table1.x=table2.x) fields, table2.y,
table2.z from table2 join table3 on table2.aa=table3.bb where
table2.xx=10
Comments
Post a Comment