Skip to content
Advertisement

Change query LEFT JOIN to FULL OUTER JOIN (respecting null values) – how to change this (without horrible performance afterwards)?

Having an Entity-Attribute Value setup table which is structured like this (it is from a third party plugin, I can not change the database design):

Table

Now I want to create a table whith data_id being the id, the names being the columns and the values being their values. Still every data_id does not have a value for every name therefore I want the value in the result to be NULL (or empty) in case there is no value for this name within the original table.

Now I have written a PHP script which is generating the required query for me:

This is how a generated query looks like:

The result table is generated correctly, but the result is empty:

result table

The reason is that results which do not have ALL column values are filtered out, but they should have null values instead (having exactly one result for each data_id existing in the table). I was thinking about replacing all LEFT JOINs with FULL OUTER JOINs (which has to be faked in MySQL), but this is overcomplicating things and the probably already bad performance will be extremly bad than. How could I solve this?

Advertisement

Answer

MySQL does not support FULL OUTER JOIN. Instead, use aggregation:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement