Forum Discussion

Yulia-Anaplan's avatar
5 years ago
Solved

Liql query with OR/AND

Hi,

I am trying to create a query that will pull articles with specific labels.
For example, I'd like to show only articles that have labels 'Cats' or 'Dogs' or 'Birds' or any different combinations of these 3 labels.

My query is:

SELECT * FROM messages WHERE (labels.text='Cats' OR labels.text='Dogs' OR labels.text='Birds')


So, in this case OR works like AND and I am getting only articles where all these labels are included. 
I am not getting articles with only 'Cats' labels or only 'Dogs' labels.

I am wondering if it is an expected behavior of OR and how it is different from AND, and if there is a way to pull articles with at least one of these labels without running each separate query for each label?

Thank you,
Yulia

3 Replies

  • SuzieH's avatar
    SuzieH
    Khoros Alumni (Retired)
    5 years ago

    Hi Yulia-Anaplan. OR has a fairly limited use within LiQL. As you saw earlier, OR will act as an AND in the use case you tried. Generally, OR is supported in conjunction with MATCHES. Keyword Searches in a LiQL Query shows an example. I see that labels.text does support MATCHES when used as a constraint on messages collection queries. That might get you closer to what you need.

  • Hi Matt,

    Thank you for your response and help.
    IN works as expected and does the job, yay.

    I found a few different components with OR in our community and I am wondering if it always worked this way or something happened with OR and now it should be all changed using IN....

  • MattV's avatar
    MattV
    Khoros Staff
    5 years ago

    Using the IN keyword should get what you need.

    So try:

    SELECT * FROM messages WHERE labels.text IN ('Cats','Dogs','Birds')