Comparing fields: Where fieldA > fieldB
riseoflex88 opened this issue · 3 comments
riseoflex88 commented
With the "andWhere", I'm struggling to work out how to do a comparison of one column vs another column.
$example->andWhere(field('price_a')->lt('price_b'));
which produces:
AND price_a > "price_b"
Which isn't quite right in this case since it's a column comparison. I'm looking at the criteria and expressions and feeling quite dumb that I just can't work out how to apply them to this case.
Any suggestions?
riseoflex88 commented
Huzzah, got it!
$example->andWhere(criteria('price_a < price_b'));
shadowhand commented
That will work but it will not escape identifiers properly. This is the proper way to use field()
with two identifiers:
// WHERE "a" < "b"
$select->where(field('a')->lt(identify('b'));
riseoflex88 commented
Ahh, ok nice.