mysql - How to use my horizontal query result to another SQL query -
i need use horizontal query result sql query. how it, please help. suppose have 2 tables:
tab1: ________________________________________ |id | arid | seq1 | seq2 | seq3 | seq4 | |___|______|______|______|______|______| |a1 | ar01 | | c | b | d | |a2 | ar02 | c | d | | b | |___|______|______|______|______|______| tab2: _____________________ |arid | seqn | code | |_____|______|______| |ar01 | | 1 | |ar01 | g | 4 | |ar01 | b | 2 | |ar01 | d | 3 | |ar01 | c | 8 | |ar01 | f | 6 | |ar01 | e | 5 | |ar01 | h | 7 | |ar02 | | 1 | |ar02 | h | 8 | |_____|______|______|
well need result this:
resulttab: _____________________ |arid | seqn | code | |_____|______|______| |ar01 | | 1 | |ar01 | c | 8 | |ar01 | b | 2 | |ar01 | d | 3 | |_____|______|______|
it fetch seq1
, seq2
, seq3
, seq4
arid
tab1
, return mapped values tab2
arid
.
please me. thank you...
please use union all
benefit of index.
select t1.arid, t1.seq1 seqn tab1 t1 inner join tab2 t2 on t1.arid = t2.arid t1.arid = 'ar01' , t1.seqn = t2.seq1 union select t1.arid, t1.seq2 seqn tab1 t1 inner join tab2 t2 on t1.arid = t2.arid t1.arid = 'ar01' , t1.seqn = t2.seq2 union select t1.arid, t1.seq3 seqn tab1 t1 inner join tab2 t2 on t1.arid = t2.arid t1.arid='ar01' , t1.seqn = t2.seq3 union select t1.arid, t1.seq4 seqn tab1 t1 inner join tab2 t2 on t1.arid = t2.arid t1.arid='ar01' , or t1.seqn = t2.seq4
Comments
Post a Comment