user969068
user969068

Reputation: 2943

Mysql query returning result even table is empty

i am using this query to get result sorted by most content_id, problem is even if request table is empty it is still returning me 1 element from content table:

select `c`.`id`, `c`.`title`, `c`.`slug`, `c`.`year`, `c`.`category`, `r`.*
, count(*) from requests as r
RIGHT JOIN `content` as c ON `r`.`content_id` = `c`.`id`
group by content_id order by count(*) desc
LIMIT 10

And if there are some records like 5 records in request table, it is returning me 6 records. First record is not in requests table. Can someone help me please? Thanks

Upvotes: 0

Views: 398

Answers (2)

hodl
hodl

Reputation: 1420

Trying using INNER JOIN instead of RIGHT JOIN

Upvotes: 1

John Conde
John Conde

Reputation: 219824

When you use an aggregate function like COUNT() you're always going to get a row returned so the database can report the result of that function, even if it is zero.

Upvotes: 0

Related Questions