Building a DbCommand to compare two columns on Variants I am trying to compare two fields located on Variants in Commerce. One is the price field and then I also have an original price field which is a custom field. I want to compare and get all variants where the price is less than the original price.

$query = craft()-&gt;db-&gt;createCommand()
            -&gt;from('commerce_variants')
            -&gt;where("price &lt; field_originalPrice")
            -&gt;queryAll();

return $query;


This is what I have so far but could be way off on this since that custom field is actually on the content table.

Any help would be awesome! Thanks!
Completely untested, but this should return all variants whose price column's value is less than your custom field's originalPrice value.

$results = craft()-&gt;db-&gt;createCommand()
    -&gt;select('v.*')
    -&gt;from('commerce_variants v')
    -&gt;join('content c', 'v.id = c.elementId')
    -&gt;where('v.price &lt; c.field_originalPrice")
    -&gt;queryAll();