<p>Yes but not like you tried it. You have to work on Yii2 level and not with Crafts Query</p>

<pre><code>// prefix for content column tables
// if you want to select fields in the field layout do
// $prefix . 'fieldHandle'
$prefix = Craft::$app-&gt;getContent()-&gt;contentTable . '.' . Craft::$app-&gt;getFields()-&gt;oldFieldColumnPrefix; 

$user = User::find()
    -&gt;andWhere(
        new OrCondition(
            ['&gt;','users.userLevel', 3],
            ['=', 'users.group', 'admin'] 
        )
    )
    -&gt;all();
</code></pre>

<p>However: keep in mind this SQL will likely produce errors because there is neither a <code>users.group</code> column nor a <code>users.userLevel</code> column in the database</p>

<p>Just as an example for more complex search:</p>

<pre><code>$prefix = Craft::$app-&gt;getContent()-&gt;contentTable . '.' . Craft::$app-&gt;getFields()-&gt;oldFieldColumnPrefix;
$criteria = [
    'where'     =&gt; new OrCondition(
        [
            [
                'between',
                $prefix . 'startDate',
                Db::prepareDateForDb($startDateDb),
                Db::prepareDateForDb($endDate)
            ],
            new AndCondition(
                [

                    ['=', $prefix . 'recurring', 1],
                    ['&lt;=', $prefix . 'startDate', $startDateDb-&gt;format('Y-m-d H:i:s')]
                ]
            )
        ]
    ),
    'orderBy'   =&gt; ['startDate' =&gt; SORT_ASC],
    'sectionId' =&gt; 10,
];

$query = Entry::find();
\Craft::configure($query, $criteria);
</code></pre>

CC BY-SA 4.0
2018-05-03T22:04:44.657Z
Thank you. But I know nothing about Yii, so in this way, how can I use relatedTo function?
yu xia
CC BY-SA 4.0
2018-05-03T23:04:59.220Z
You have to join the `relation` table and check if a value exists with your criteria. You can perform a simple query to see what Craft does. `Entry::find()->relatedTo(1)->getRawSql()`
Robin Schambach
CC BY-SA 4.0
2018-05-03T10:47:36.173Z
2018-05-03T10:53:20.813Z
2018-05-03T10:53:20.813Z
Robin Schambach
<p>it is possible to do like this:</p>

<pre><code>$users = Entry::find()
  -&gt;userLevel('&gt;3')
  -&gt;or group('admin')
</code></pre>

<p>So in database level,the sql will go like this:</p>

<pre><code>select * from users
  where users.userLevel &gt; 3 or users.group='admin'
</code></pre>

<p>Many many thanks!!</p>

<entry><entrymodel><query><elementquery>
How to get entries with or
# How to get entries with or

(Asked by: username_0 on 2018-05-03)

it is possible to do like this:
$users = Entry::find()
  ->userLevel('>3')
  ->or group('admin')

So in database level,the sql will go like this:
select * from users
  where users.userLevel > 3 or users.group='admin'

Many many thanks!!

**Tags:** entry, entrymodel, query, elementquery

---

## 1 answers

### Answer by username_1 on 2018-05-03. Score: 1

Yes but not like you tried it. You have to work on Yii2 level and not with Crafts Query
// prefix for content column tables
// if you want to select fields in the field layout do
// $prefix . 'fieldHandle'
$prefix = Craft::$app->getContent()->contentTable . '.' . Craft::$app->getFields()->oldFieldColumnPrefix; 

$user = User::find()
    ->andWhere(
        new OrCondition(
            ['>','users.userLevel', 3],
            ['=', 'users.group', 'admin'] 
        )
    )
    ->all();

However: keep in mind this SQL will likely produce errors because there is neither a users.group column nor a users.userLevel column in the database
Just as an example for more complex search:
$prefix = Craft::$app->getContent()->contentTable . '.' . Craft::$app->getFields()->oldFieldColumnPrefix;
$criteria = [
    'where'     => new OrCondition(
        [
            [
                'between',
                $prefix . 'startDate',
                Db::prepareDateForDb($startDateDb),
                Db::prepareDateForDb($endDate)
            ],
            new AndCondition(
                [

                    ['=', $prefix . 'recurring', 1],
                    ['<=', $prefix . 'startDate', $startDateDb->format('Y-m-d H:i:s')]
                ]
            )
        ]
    ),
    'orderBy'   => ['startDate' => SORT_ASC],
    'sectionId' => 10,
];

$query = Entry::find();
\Craft::configure($query, $criteria);

> **Comments:**
> **username_0**: Thank you. But I know nothing about Yii, so in this way, how can I use relatedTo function?  
> **username_1**: You have to join the `relation` table and check if a value exists with your criteria. You can perform a simple query to see what Craft does. `Entry::find()->relatedTo(1)->getRawSql()`  

---

