我的表offers
包含五个相关列offer_id
,offer_type
,offer_amount
,end_date
和offer_status
我正在尝试选择与offer_amount
匹配的offer_type
最高的行和{1}的offer_status
行(有效)。
我正在使用的查询是
SELECT * FROM offers_tbl WHERE offer_status = 1 AND offer_type = 'site-wide' AND offer_amount = (SELECT MAX(offer_amount) FROM offers_tbl )
如果MAX(offer_amount)的offer_status
恰好为0(无效),则不会返回任何结果。
如果具有最高MAX(offer_amount)
的特定行恰好具有1的offer_status,则此方法可以正常工作。只有当报价状态为0时才会中断。
答案 0 :(得分:2)
尝试让它从活动状态中找到MAX:
SELECT * FROM offers_tbl WHERE offer_status = 1 AND offer_type = 'site-wide' AND offer_amount = (SELECT MAX(offer_amount) FROM offers_tbl WHERE offer_status = 1)
否则,它发现MAX提供的金额可能为0,这使得外部查询找不到状态为1和该金额的匹配。