I'm having an issue with following query:
SELECT
badge.name AS badge_name, badge.description, badge.type, badges.time, user.name AS user_name
FROM
badges LEFT JOIN badge ON badges.badge_name = badge.name LEFT JOIN user ON user.id=badges.user_id
WHERE
user.name IS NOT NULL
ORDER BY badges.time DESC
LIMIT 5
Now, I'd like to check that the amount of results is not 0, so I check like I always do with by adding this after the SELECT
: count(1) AS counter
. However, this influences the results.
I've seen that this might be an issue due to also having a LIMIT
, but what's the most efficient way to circumvent this? I just want to check whether there are any results returned or not, to display a proper message it there are no results. I'm using PDO
, but since it's a SELECT
i can't use the ->rowCount()
to check the amount of rows returned.
EDIT:
I want to determine whether there are any results, yes or no. My normal way of doing so is using count(1) AS counter
, and checking the value of the counter as follows:
while($row['counter'] = $STH->fetch()){
if($row['counter'] == 0){
// Error message
}else{
echo $row['badge_name'] . "etc...";
}
}
However, this seems to mess up the results due to the LIMIT
(check the SQLFiddles).
So: how can I check this, preferably in a single query?