0

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. Commented Aug 4, 2019 at 19:45

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, the IF statement worked fine. Thank you very much.
The answer has helped me very much. It says I am supposed to click on a green check mark to the right. There is no green check mark anywhere on my screen, so I am sending this note to accept the answer and to close the thread.
Yes, I think I got it to work, the check mark is showing now and it is showing green. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.