Answers.Body: <p>Please don't laugh, but I thought to myself, let's give this a go in Twig and see where it takes me.
  It very soon began to become clear, that this will end up in lots of criteria models, db queries and a huge pile of Twig code.</p>
  
  <p>So my better answer should be, don't do it in Twig. This is something you should better do in PHP with
  custom database queries (→ <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder" rel="nofollow">Yii query builder</a>). But now that I've done it, here's <a href="https://gist.github.com/carlcs/adbb84b5521f7d010c8e" rel="nofollow">a gist</a> of that Twig code.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-04T12:00:09.250Z
Answers.Text: Thanks for taking the time to put that together. I was worried that it will involve clunky Twig code, and was thinking about reducing it into a plugin. Got an example on how to do this with the query builder?
Answers.UserDisplayName: Fred Carlsen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-04T12:13:57.217Z
Answers.Text: Unfortunatelly there's only very few examples here on Stack Exchange. This might help: http://craftcms.stackexchange.com/q/7175 or this one: http://craftcms.stackexchange.com/q/4600.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-04T07:45:12.030Z
Answers.LastActivityDate: 2015-09-04T07:45:12.030Z
Answers.UserDisplayName: carlcs
Body: <p>I got users that has 3 Entries fields in their profile: breadForMonday, breadForWednesday and breadForFriday.</p>
  
  <p>I want to differentiate between users with 1, 2 or 3 selected entries. How would I use relatedTo together with and/or and :empty/:notempty?</p>
  
  <p>Something like this:</p>
  
  <pre><code>{% set bread = craft.entries({ section: 'bread' }).limit(null) %}
  {# Find users that have selected entries for two out of three fields #}
  {% set users = craft.users({
      group: 'customers',
      relatedTo: [
          'and',
          { targetElement: bread, field: 'breadForMonday' },
          { targetElement: bread, field: 'breadForWednesday' }
      ]
  }) %}
  </code></pre>
  
Tags: <users><relations><queries>
Title: Using relatedTo, and/or and :empty/:notempty in queries
ThreadText: # Using relatedTo, and/or and :empty/:notempty in queries
  
  (Asked by: username_0 on 2015-09-03)
  
  I got users that has 3 Entries fields in their profile: breadForMonday, breadForWednesday and breadForFriday.
  
  I want to differentiate between users with 1, 2 or 3 selected entries. How would I use relatedTo together with and/or and :empty/:notempty?
  
  Something like this:
  
  ```
  {% set bread = craft.entries({ section: 'bread' }).limit(null) %}
  {# Find users that have selected entries for two out of three fields #}
  {% set users = craft.users({
      group: 'customers',
      relatedTo: [
          'and',
          { targetElement: bread, field: 'breadForMonday' },
          { targetElement: bread, field: 'breadForWednesday' }
      ]
  }) %}
  
  ```
  
  **Tags:** users, relations, queries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-09-04. Score: 3
  
  Please don't laugh, but I thought to myself, let's give this a go in Twig and see where it takes me.
  It very soon began to become clear, that this will end up in lots of criteria models, db queries and a huge pile of Twig code.
  
  So my better answer should be, don't do it in Twig. This is something you should better do in PHP with
  custom database queries (→ [Yii query builder](http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder)). But now that I've done it, here's [a gist](https://gist.github.com/username_1/adbb84b5521f7d010c8e) of that Twig code.
  
  > **Comments:**
  > **username_0**: Thanks for taking the time to put that together. I was worried that it will involve clunky Twig code, and was thinking about reducing it into a plugin. Got an example on how to do this with the query builder?  
  > **username_1**: Unfortunatelly there's only very few examples here on Stack Exchange. This might help: http://craftcms.stackexchange.com/q/7175 or this one: http://craftcms.stackexchange.com/q/4600.  
  
  ---
  
  