1

How is it possible to write a couchbase request in order to choose the first condition in where if it is defined or the second one if the first one is undefined.

I tried somethings like :

SELECT id FROM auto as a WHERE IFMISSINGORNULL(ARRAY_LENGTH(a.data[0])>1, ARRAY_LENGTH(a.data[1])>1)

In this request if a.data[0] is defined, I want to check the length of a.data[0] and if a.data[0] is not defined, I want to check the length of a.data[1].

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
Mo0nKizz
  • 149
  • 7
  • 1
    Is there a reason you can't write something of the form `WHERE (x = true AND foo) OR (x = false AND bar)`? – Matthew Groves Oct 21 '21 at 14:24
  • Something like ``` SELECT id FROM auto as a WHERE (a.data[0] IS NOT NULL AND ARRAY_LENGTH(a.data[0])>1) OR (a.data[0] IS NULL AND ARRAY_LENGTH(a.records[1].changes.update_date)>1) ``` ?? – Mo0nKizz Oct 21 '21 at 14:29
  • 1
    Something of that form, yes. I don't quite understand why you're using `ARRAY_LENGTH`, though. Maybe it would be better to post a new question about what you're trying to query in the first place. – Matthew Groves Oct 21 '21 at 15:05

1 Answers1

0

Condition should look as follows

WHERE ARRAY_LENGTH(IFNULL(a.data[0], a.data[1])) > 1

IFNULL(a.data[0], a.data[1])

  • returns first argument if it is NOT NULL
  • second argument when first one is NULL
vsr
  • 7,149
  • 1
  • 11
  • 10