Forum Discussion

Yulia-Anaplan's avatar
4 years ago

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

  • MattV's avatar
    MattV
    Khoros Staff

    Using the IN keyword should get what you need.

    So try:

    SELECT * FROM messages WHERE labels.text IN ('Cats','Dogs','Birds')
    • 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....