Merge two complex MYSQL queries -


i want on way merge 2 complex mysql queries.

query 1:

select  p.*    posts p     inner join     (         select  a.id            posts                 inner join post_tags b                     on a.id = b.post_id           a.post '%mmmm%' ,                 b.tagname in ('#test','#iseeyou')         group   id         having  count(distinct b.tagname) = 2     ) sub on p.id = sub.id order   p.upvotes desc, p.unix_timestamp desc 

query 2:

select p.*, ((upvotes + 1.9208) / (upvotes + downvotes) - 1.96 * sqrt((upvotes * downvotes)  / (upvotes + downvotes) + 0.9604) / (upvotes + downvotes))  / (1 + 3.8416 / (upvotes + downvotes)) ci_lower_bound   posts p upvotes + downvotes > 0   , p.unix_timestamp between 1363023402 , 1363109802 order ci_lower_bound desc 

a small table definition given @ sql fiddle

actually, first 1 search query , second 1 gives popular results based on votes in last 24 hours, want use search query based on formula used in second 1 , time range

with minimal changes (if have understood want correctly)

select  p.*, ((upvotes + 1.9208) / (upvotes + downvotes) - 1.96 * sqrt((upvotes * downvotes)  / (upvotes + downvotes) + 0.9604) / (upvotes + downvotes))  / (1 + 3.8416 / (upvotes + downvotes)) ci_lower_bound     posts p     inner join     (         select  a.id            posts inner join post_tags b  on a.id = b.post_id           a.post '%mmmm%' , b.tagname in ('#test','#iseeyou')         group   id         having  count(distinct b.tagname) = 2     ) sub on p.id = sub.id upvotes + downvotes > 0  , p.unix_timestamp between 1363023402 , 1363109802  order ci_lower_bound desc 

possibly touch more efficient swap around clause on subselect (the leading % on not use index, hence more efficient join against indexed check on post_tags))

select  p.*, ((upvotes + 1.9208) / (upvotes + downvotes) - 1.96 * sqrt((upvotes * downvotes)  / (upvotes + downvotes) + 0.9604) / (upvotes + downvotes))  / (1 + 3.8416 / (upvotes + downvotes)) ci_lower_bound     posts p     inner join     (         select  a.id            post_tags b straight_join posts on a.id = b.post_id           a.post '%mmmm%' , b.tagname in ('#test','#iseeyou')         group   id         having  count(distinct b.tagname) = 2     ) sub on p.id = sub.id upvotes + downvotes > 0  , p.unix_timestamp between 1363023402 , 1363109802  order ci_lower_bound desc 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -