SQL returning NULL instead of empty result

Adam

SQL FIDDLE HERE

I am making a simple blog system where posts can have more than one category, so I have this query:

SELECT *, GROUP_CONCAT(item_category) AS item_categories
FROM (`dev_pages`)
LEFT JOIN `dev_items_to_categories` ON `dev_items_to_categories`.`item_id` = `dev_pages`.`page_id`
WHERE deleted_time IS NULL
AND `page_type` =  'blog'
AND `item_category` =  '16'
ORDER BY `page_title` ASC

It works fine but if there are no results instead of coming back as nothing it returns NULL or the default value (See SQL Fiddle)

I managed to make a around but I was wondering if anyone had a better solution to this:

SELECT a.* FROM (

    SELECT *, GROUP_CONCAT(item_category) AS item_categories
    FROM (`dev_pages`)
    LEFT JOIN `dev_items_to_categories` ON `dev_items_to_categories`.`item_id` = `dev_pages`.`page_id`
    WHERE deleted_time IS NULL
    AND `page_type` =  'blog'
    AND `item_category` =  '16'
    ORDER BY `page_title` ASC

) AS a
WHERE page_id > 0 
Barmar

You need a GROUP BY clause:

SELECT *, GROUP_CONCAT(item_category) AS item_categories
FROM (`dev_pages`)
LEFT JOIN `dev_items_to_categories` ON `dev_items_to_categories`.`item_id` = `dev_pages`.`page_id` AND `item_category` =  '16'
WHERE deleted_time IS NULL
AND `page_type` =  'blog'
GROUP BY dev_pages.page_id
ORDER BY `page_title` ASC

You also need to put item_category = '16' into the ON clause. Otherwise, you will filter out rows with no matches in dev_items_to_categories; since you're using LEFT JOIN rather than INNER JOIN, I assume you want those rows with null matches.

FIDDLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Returning varchars that are not null or empty SQL

Weird issue with returning empty struct instead of NULL

Postgres returning empty result if one of the outcome is null

SQL result returning total rows instead of specified

Golang Insert NULL into sql instead of empty string

Why SQL function always returning empty result set?

SSIS Execute SQL Task Stored Procedure returning empty result sets

returning null if literal is empty

Returning 1 or 0 instead of value or null in T-SQL

Variable returning 'null' instead of null

GetComponent returning "null" instead of null

Pandas iloc returning empty result

Pouchdb view returning empty result

JDBC returning empty result set

FireDac query returning empty result

MySQL returning an empty result set

CollectionGroup Query returning an empty result

SQL returning empty JSON brackets when no value found instead of empty "value"

SQL statement returning not result

SQL split string and get NULL values instead of empty string

Oracle 11 PL/SQL: check variable for null, empty string and no result

Hibernate query.list() method is returning empty list instead of null value

Laravel Relation returning null result

Regex returning null vs returning empty string

NSNull returns <null> instead of returning null

Mysql LEFT JOIN with IS NULL returning null result

Sorting MongoDB result by another MongoDB result is returning [null, null, null]

findById returning a list of documents instead of a single result

hasManyThrough is returning only one result instead of all