I would like to add a column to a result set based on whether a field field_value in the table is NULL.
Here is my query that works:
SELECT field_id, (
SELECT field_value
FROM employee_field_info
WHERE employee_id = '1'
AND field_id = persona_fields.field_id
) AS field_value, (
SELECT field
FROM FIELDS WHERE id = persona_fields.field_id
) AS field_name
FROM persona_fields
WHERE persona_id = '1'
What i want to be able to do is to add a tinyint (for a bool) column if the field_value is NULL.
I’ve tried this:
SELECT field_id, (
SELECT field_value
FROM employee_field_info
WHERE employee_id = '1'
AND field_id = persona_fields.field_id
) AS field_value, (
SELECT field
FROM FIELDS WHERE id = persona_fields.field_id
) AS field_name,
IF(field_value IS NULL, "0","1") AS value_bool
FROM persona_fields
WHERE persona_id = '1'
But that just comes up with the “Unknown column ‘field_value’ in ‘field list.’
Is there anyway i can accomplish what I’m trying to do, or am I pretty much out of luck?
You can’t reference columns in the parent
SELECTfrom a sub-select. The sub-select is basically run in isolation. What you need to do is re-write this as aJOIN. Your query is quite convoluted and hard to follow, and I think incomplete, so I can only approximate a solution.Here’s an example: