Returning NULL

Asked

Viewed 126 times

1

I am using this query:

SELECT GROUP_CONCAT(coluna1) as valores FROM arquivos WHERE coluna2 IN (21, 22)

It returns column1 values of lines whose column2 has one of the specified values.

The problem is that, in this case, the value 21 is not found in the table. So I need to returnate a NULL. But the query is only returning the value found.

  • 1

    The query will only return the values found , to return a null you need to resort to tricks like Union etc

1 answer

1

I don’t know if this is exactly what you need, but take a test:

select group_concat(ifnull(coluna1, 'x')) as valores
  from (select 21 coluna2 
        union
        select 22
       ) tab_aux
  left join arquivos
    on arquivos.coluna2 = tab_aux.coluna2

Note: x represents your null'.

  • That’s exactly what I need. But there’s a catch: the ids I use come from a dynamic array.

  • Let me get this straight: 21 and 22 are not fixed values, they are generated dynamically, this? And they are always 2 values only?

  • that’s right: they are not generated dynamically. And the number of values may vary.

  • Dude, so there’s no way, you’ll have to build this query also dynamically, per line of code. If you want to show your show like it is today maybe I or someone else can help you better.

  • Dude, so I’ll try to rearrange the tables. But thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.