在我的模型Listing
中,除了其他列之外,我还有这两个列:
- seller_currency
- bidder_currency
listings.seller_currency
和listings.bidder_currency
的值可以是1
(USD)或0
(EUR)。
我试图得到所有的上市,其中seller_currency
和bidder_currency
是0
(欧元)或至少有一个是0
(欧元)。
还有比这更好的写条件的方法吗?
Listing.where('(seller_currency=0) OR (bidder_currency=0) OR (seller_currency=0 AND bidder_currency=0)')
2条答案
按热度按时间wgeznvg71#
根据你的例子,至少满足一个条件就足够了。
nbewdwxp2#
你只需要一个或条件,它涵盖了它们都为0的情况。这种方式更可取,因为它是一个更快的查询,因为它同时计算两个列。此外,如果你想使用变量,问号会停止sql注入。
Listing.where("(seller_currency=?) OR (bidder_currency=?)",0,0)