Search code examples
mysqlifnull

MYSQL: Stop IFNULL returning "empty" rows of null


I'm using IFNULL in the following way:

SELECT name, address, birthday, IFNULL(GROUP_CONCAT(pet_name), 'none') AS `pets_names` FROM pets WHERE user_id = 1;

However, If I have no user 1, and therefore no results, I will get a single row where all fields are null apart from pet_names which will say 'none'. I tried setting it to only give a value if user_id IS NOT NULL but then I got a row with every value as NULL. How can I get it to just not return any rows when using IFNULL?


Solution

  • I've fixed this by adding

    GROUP BY name
    

    I presume because there are no names, there's nothing to group by and thus no results.