Multiple AND / OR Conditions for WHERE CONTAINS

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

I typically prefer inline Tags. (e.g. Tag:: Red, Blue, Purple, Grey)
Can someone share the syntax (or link to reference) for nested conditions? i.e. Where Re AND (Blue OR Purple)

Things I have tried

TABLE without ID as "Name", Tag
WHERE contains(Tag,"Red") AND (contains(Tag,"Blue") OR Contains(Tag,"Purple"))
FLATTEN Name Sort file.mtime desc

To clarify, I want Red (Must have) AND either Blue or Purple. When I try above, it doesn’t work. When I remove the parentheses, it makes it too broad (i.e. Blue or Purple regardless of Red, which isn’t what I want either).

There are few things I question about that query and first of all that is the re-use of Tag as it might clash with the predefined tags field related to notes. Do try with another field name. tag and tags are kind of reserved for the file tags, so it might give you some issues when also used as an inline fields. It just looks a little strange.

Secondly, why do you do the FLATTEN name? What’s the purpose of that one?

Thirdly, given a definition like: Tag:: red, navyblue, seagreen, purple the field Tag holds a string, not an array of different values. This can lead to unwanted side-effects in some cases. Especially when used in together with contains(). It’ll then happily match the above string against both blue and green, as they’re part of the string, although not unique colors.

Your syntax related to the actual AND & OR seems correct, so that’s not the part of the query which is causing you issues.

myTag:: red, bluish, green, purple

typeof(myTag), myTag,
contains(myTag, "red")
AND (contains(myTag, "blue") OR 
      contains(myTag, "green")) as "bool?"
WHERE file = this.file

Not corrected for the string issue, this returns the correct result. Do however note that if you just change the field definition, the query will not automatically update since the query hasn’t changed. This is a known dataview bug, and can be countered by adding/removing a space in the query to get it to refresh. (Another solution could be to have a rebuild button to rebuild the entire view, this’ll also cause the query to refresh. Some posts have been on this topic lately)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.