<p>You could just do (Assuming your field1 and field2 are set to numbers as Field type):</p>

<pre><code>craft.entries.section('mySection').all()
</code></pre>

<p>as your query then within your loop:</p>

<pre><code>{% for entry in entries %}
    {% if entry.field1 &gt; entry.field2 %}
        // Show the entry
    {% endif %}
{% endfor %}
</code></pre>

<p>It depends on what you need but this would work.</p>

CC BY-SA 4.0
2019-08-02T19:16:11.983Z
This seems horrible inefficient, return ALL entries when you only need certain ones. Is there no other way?
Gary Reckard
CC BY-SA 4.0
2019-01-14T22:51:16.900Z
2019-01-14T22:51:16.900Z
Oli
<p>I agree that any solution where you run a query and then conditionally render the result isn't very efficient.</p>
<p>You can do this in SQL by using the <code>where()</code> condition.</p>
<pre><code>craft.entries
    .section('mySection')
    .where('content.field_field1 &gt; content.field_field2')
    .all()
</code></pre>
<p>Documentation on the <code>where()</code> condition can be found <a href="https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2021-04-27T07:31:00.603Z
Is the syntax for the `.where()` clause in your example documented anywhere? Especially the `content` table and and `field_` prefix for custom fields. I can only find the [Yii framework documentation](https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail), but of course that doesn't tell you anything about Craft's database structure, and [Craft's own documentation](https://craftcms.com/docs/3.x/element-queries.html#advanced-element-queries) is sorely lacking regarding those advanced queries ...
MoritzLost
CC BY-SA 4.0
2021-04-27T08:00:57.157Z
I don't believe it is documented by Craft no, their `where()` condition [documentation](https://craftcms.com/docs/3.x/element-queries.html#conditions) links straight through to Yii, as that's where `where()` is implemented. Usually I reverse engineer this sort of thing using the `getRawSql()` [function](https://craftcms.com/docs/3.x/element-queries.html#query-execution), but agree that some more advanced query examples like this could be provided within the Craft documentation.
Matt Banner
CC BY-SA 4.0
2021-04-27T08:03:13.917Z
Yeah I agree, thanks for the reply! I'm always feeling a bit uncomfortable using undocumented code like this since it could change at any moment. Definitely something the documentation could improve on!
MoritzLost
CC BY-SA 4.0
2021-04-26T19:39:32.840Z
2021-04-27T17:13:45.340Z
2021-04-27T17:13:45.340Z
Matt Banner
<p>I would like to do something like this:</p>

<pre><code>craft.entries.section('mySection').field1('&gt; field2').all()
</code></pre>

<p>And it should return all the entries in <code>mySection</code> whose <code>field1</code> value is greater than their <code>field2</code> value.</p>

<p>Is it possible?</p>

<entries><craft3><field><elementquery>
Element query: find entries where a field's value is greater than, or less than another field's value?
# Element query: find entries where a field's value is greater than, or less than another field's value?

(Asked by: username_0 on 2019-01-14)

I would like to do something like this:
craft.entries.section('mySection').field1('> field2').all()

And it should return all the entries in mySection whose field1 value is greater than their field2 value.
Is it possible?

**Tags:** entries, craft3, field, elementquery


---

## 2 answers

### Answer by username_3 on 2021-04-26. Score: 4 | Accepted answer

I agree that any solution where you run a query and then conditionally render the result isn't very efficient.
You can do this in SQL by using the where() condition.
craft.entries
    .section('mySection')
    .where('content.field_field1 > content.field_field2')
    .all()

Documentation on the where() condition can be found here.

> **Comments:**
> **username_4**: Is the syntax for the `.where()` clause in your example documented anywhere? Especially the `content` table and and `field_` prefix for custom fields. I can only find the [Yii framework documentation](https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail), but of course that doesn't tell you anything about Craft's database structure, and [Craft's own documentation](https://craftcms.com/docs/3.x/element-queries.html#advanced-element-queries) is sorely lacking regarding those advanced queries ...  
> **username_3**: I don't believe it is documented by Craft no, their `where()` condition [documentation](https://craftcms.com/docs/3.x/element-queries.html#conditions) links straight through to Yii, as that's where `where()` is implemented. Usually I reverse engineer this sort of thing using the `getRawSql()` [function](https://craftcms.com/docs/3.x/element-queries.html#query-execution), but agree that some more advanced query examples like this could be provided within the Craft documentation.  
> **username_4**: Yeah I agree, thanks for the reply! I'm always feeling a bit uncomfortable using undocumented code like this since it could change at any moment. Definitely something the documentation could improve on!  

---

### Answer by username_1 on 2019-01-14. Score: 0

You could just do (Assuming your field1 and field2 are set to numbers as Field type):
craft.entries.section('mySection').all()

as your query then within your loop:
{% for entry in entries %}
    {% if entry.field1 > entry.field2 %}
        // Show the entry
    {% endif %}
{% endfor %}

It depends on what you need but this would work.

> **Comments:**
> **username_2**: This seems horrible inefficient, return ALL entries when you only need certain ones. Is there no other way?  

---

