mysql - Return results based on a range of values ie WHERE ColA = (ColB + n OR ColB - n) -


i'm little stuck on following, may ask direction please?

i have created 2 temp tables 'compare1' , 'compare2' , i'd list words common between tables.

however, in each of temp tables have numeric field, 'chunkidx', represents position of word. limit results list within specific range of position.

let me show results, first , explanation of wrong them ...

tspan   wlen    asize   c1_chunkidx word    tspan   wlen    asize   c2_chunkidx word 5       7       6       0           daa     5       7       6       43          daa 5       8       7       1           baa     5       8       7       44          baa 5       7       6       1           baa     5       7       6       1           baa 

of results listed above, want line 3. c2_chunkidx value between c1_chunkidx - 5 , c1_chunkidx + 5. obviously, lines 1 , 2 fail test.

i thought include ...

where (c2.chunkidx between (c1.chunkidx - 5) , (c1.chunkidx + 5)) 

or perhaps ...

where (c2.chunkidx >= (c1.chunkidx - 5) or c2.chunkidx <= (c1.chunkidx + 5)) 

in clause, doing returns nothing. literally no result tab view empty dataset (i'm using heidisql). it's query never fired, don't error message.

clearly i'm missing (other results).

my complete query looks following.

select c1.tspan, c1.wlen, c1.asize, c1.chunkidx c1_chunkidx, c1.word,        c2.tspan, c2.wlen, c2.asize, c2.chunkidx c2_chunkidx, c2.word compare1 c1     inner join compare2 c2 on c1.word = c2.word (c2.chunkidx between (c1.chunkidx - 5) , (c1.chunkidx + 5))     , c1.tspan = c2.tspan     , c1.wlen = c2.wlen     , c1.asize = c2.asize 

may ask, how can limit results within range based on value of c1.chunkidx field?

thank time.

the answer seems to use variables

select c1.tspan, c1.wlen, c1.asize, c1.chunkidx c1_chunkidx, c1.word,    c2.tspan, c2.wlen, c2.asize, c2.chunkidx c2_chunkidx, c2.word , @lowchunk := (c1.chunkidx - 5) , @highchunk := (c1.chunkidx + 5) compare1 c1     inner join compare2 c2 on c1.word = c2.word c2.chunkidx between @lowchunk , @highchunk     , c1.tspan = c2.tspan     , c1.wlen = c2.wlen     , c1.asize = c2.asize 

this seems give me need. i'm not strong sql man, can't explain why so.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -