Is there any way to shorten/easier read the following SQL to concatenate a string.
SELECT
CONCAT(
IF(location_address1 != '',
CONCAT(location_address1, ", "),
""
),
IF(location_address2 != '',
CONCAT(location_address2, ", "),
""
),
IF(location_town != '',
CONCAT(location_town, ", "),
""
),
IF(location_region != '',
CONCAT(location_region, ", "),
""
),
IF(location_postcode != '',
CONCAT(location_postcode, ", "),
""
),
c.country_name
)
FROM
countries c
WHERE
c.country_id = locations.country_id LIMIT 1
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
NULLIF(expr1,expr2)
Returns NULL if expr1 = expr2 is true, otherwise returns expr1.
SO your query could be: