0
votes

I want to present 4 fields from a table but the 4th field is based on a condition.

i.e. I want to present field1, field2, field3 and then either field4 or field5 where the choice of field5 would be based on a condition.

Is this possible?

I can present the table of field1, field2, field3 and field4 with no problem.

1
Do you want to encode the condition into the SQL or into the php code? Both are capable of handling that. It might also be helpful to edit the question to include a minimal reproducible example.Jason Aller

1 Answers

0
votes

Use a CASE expression:

SELECT
    field1,
    field2,
    field3,
    CASE WHEN <some condition> THEN field4 ELSE field5 END AS other_field
FROM yourTable;

Or, you could use MySQL's IF function:

IF(<some condition>, field4, field) AS other_field