1

I have a simple query like this that go over my mysql records of certain table and gives me jsons out of each record:

SELECT json_object(
  'personId', p.id,
  'formalName', p.name,
  'country', p.country)
FROM person p;

but I formalName can be null, and I wanted to add a condition like

if p.name is null 'NoName' else p.name

is it possible?

1 Answer 1

2

Yeap change p.name to case when p.name is null then 'NoName' else p.name end at the end your query will be:

SELECT json_object(
  'personId', p.id,
  'formalName', case when p.name is null then 'NoName' else p.name end,
  'country', p.country)
FROM person p;
Sign up to request clarification or add additional context in comments.

Comments

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.