0

I have this select:

select 'fv' prefix, c.user, MAX(f.data) as data, c.user, count(c.user) as logs
from favorites f inner join
     cadastro c
     ON f.user = c.id
where f.fv = '1' and c.user <> f.user and f.data > now()

If I have no result to show, mysql shows this:

prefix - user - data - user - logs
fv NULL NULL NULL 0

I don't want to show when there is no results. What is wrong? is the count(c.user)? how can I avoid count to count 0?

1 Answer 1

2

An aggregation query with no group by always returns one row. One simple method is to add an aggregation:

select 'fv' as prefix, c.user, MAX(f.data) as data, c.user, count(c.user) as logs
from favorites f inner join
     cadastro c
     ON f.user = c.id
where f.fv = '1' and c.user <> f.user and f.data > now()
group by f.fv;

If there are no matches, this will not return any rows.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.