Mysql – LEFT JOIN DEFAULT NULL VALUE

join;MySQLnull

My query can be simplified as this:

SELECT a.id, a.name, b.* FROM a LEFT JOIN b ON a.id=b.id

Which b is from group concat select (very dynamic column). Sometimes there are 3 columns in b, sometimes 10 columns depend on filtering.

What I want is something like this:

SELECT a.id, a.name, IFNULL(b.*,0) FROM a LEFT JOIN b ON a.id=b.id

I already looked here and here and the answer is always repeating per column. But I can't apply ifnull per column because I don't know what the columns are. Is there any other approach for replacing null values from left join?

All columns are of the integer type.

Best Answer

If I understand correctly, you could have any number of columns from table B? If so, then you will need to dynamically create the SQL statement (by querying the information_schema table) and just wrap each column from B in your ifnull(field, ifnullvalue).

Related Question