mysql - How can I exclude posts whose total votes are less than 1? -


here query:

select count(1)  qanda question  join qanda answer on question.id = answer.related answer.related not null   , answer.author_id = ?   , question.amount null   , answer.date_time >= unix_timestamp(now() - interval 1 year) , unix_timestamp(now() - interval 1 hour)   , answer.id not in (       select post_id       votes       table_code = 15       group post_id       having sum(value) < 0 ) 

my query returns number of user's answers have either 0 or positive total votes (total votes: 0, 1, 2, ...). need exclude answers have 0 total votes.

therefore replace:

... having sum(value) < 0 

with

... having sum(value) < 1 

but doesn't work expected. mean still counts answers have 0 total votes. what's wrong? , how can fix it?

the problem inner query doesn't select posts have no votes, aren't excluded. there couple of ways of solving this, easiest reverse logic exclusion inclusion, ie "show posts have total votes > 1":

select count(1)  qanda question  join qanda answer on question.id = answer.related answer.related not null   , answer.author_id = ?   , question.amount null   , answer.date_time between unix_timestamp(now() - interval 1 year) , unix_timestamp(now() - interval 1 hour)   , answer.id in ( -- remove "not"       select post_id       votes       table_code = 15       group post_id       having sum(value) > 0 ) -- change > 0 

as join:

select count(*) (   select answer.id   qanda question   join qanda answer on question.id = answer.related   join votes on votes.post_id = answer.id        , votes.table_code = 15    answer.related not null    , answer.author_id = ?    , question.free null    , answer.date_time between unix_timestamp(now() - interval 1 year)      , unix_timestamp(now() - interval 1 hour)    , answer.timestamp > subdate(now(), 365)    group answer.id   having sum(votes.value) > 1) x 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -