多个AND / OR运算符在mysql查询中没有按预期工作

时间:2016-06-17 17:17:00

标签: php mysql

我正在尝试查询我的项目表并根据搜索查询获取项目,但我想从这些结果中排除状态为2或0的项目,1表示可供出价的项目。

到目前为止,我的查询是

SELECT a.item_id,a.auction_id as item_auction_id,a.item_code,a.name as item_name,a.description as item_desc,a.full_description as item_full_desc,a.image as item_image,a.donor as item_donor,a.cost as item_cost,a.value as item_value,a.reserve as item_reserve,a.bid_increment as item_bid_increment,a.buy_price as item_buy_price,a.status as item_status,a.published as item_published,a.paid as item_paid,a.pay_type as item_pay_type, a.transaction_id as item_transaction_id,a.is_raffle as item_is_raffle,a.raffle_price as item_raffle_price, a.raffle_qty as item_raffle_qty,a.live_auction as item_live_auction,a.active as item_active,a.refunded as item_refunded, MAX(b.bid_amt) as item_high_bid,b.user_id as item_high_bid_user_id,b.bid_time as item_high_bid_time, b.bid_id as item_high_bid_id,SUM(c.raffle_ticket_qty) as item_tickets_purchased, '0000,0000' as item_bid_history, 0 as watching
        FROM text2bid_items as a
        LEFT JOIN (SELECT user_id, item_id, MAX(bid_amt) as bid_amt, bid_time, bid_id FROM text2bid_bids GROUP BY bid_id) as b
        ON a.item_id = b.item_id
        LEFT JOIN text2bid_raffle_purchases as c
        ON a.item_id = c.item_id
        WHERE a.name LIKE '%$params[2]%'
        OR a.description LIKE '%$params[2]%'
        OR a.full_description LIKE '%$params[2]%' 
        AND a.status = 1
        GROUP BY a.item_id

但状态= 2或0的项目仍显示在我的结果中。我确定这与ORAND的使用有关,但我不知道如何谷歌这样做以弄清楚如何做到这一点,我试过了()的不同组合,看看我是否可以获得差异优先权,但也许这不是解决方案?

结果应该是name, description, or full_description与搜索查询类似的任何项目,但我想要排除没有status等于1的项目

1 个答案:

答案 0 :(得分:1)

查询中的逻辑运算符是混合的。因此最好将它们保留在括号中,以便保持查询逻辑。 我已相应更新了查询,请检查!

SELECT a.item_id,a.auction_id as item_auction_id,a.item_code,a.name as item_name,a.description as item_desc,a.full_description as item_full_desc,a.image as item_image,a.donor as item_donor,a.cost as item_cost,a.value as item_value,a.reserve as item_reserve,a.bid_increment as item_bid_increment,a.buy_price as item_buy_price,a.status as item_status,a.published as item_published,a.paid as item_paid,a.pay_type as item_pay_type, a.transaction_id as item_transaction_id,a.is_raffle as item_is_raffle,a.raffle_price as item_raffle_price, a.raffle_qty as item_raffle_qty,a.live_auction as item_live_auction,a.active as item_active,a.refunded as item_refunded, MAX(b.bid_amt) as item_high_bid,b.user_id as item_high_bid_user_id,b.bid_time as item_high_bid_time, b.bid_id as item_high_bid_id,SUM(c.raffle_ticket_qty) as item_tickets_purchased, '0000,0000' as item_bid_history, 0 as watching
            FROM text2bid_items as a
            LEFT JOIN (SELECT user_id, item_id, MAX(bid_amt) as bid_amt, bid_time, bid_id FROM text2bid_bids GROUP BY bid_id) as b
            ON a.item_id = b.item_id
            LEFT JOIN text2bid_raffle_purchases as c
            ON a.item_id = c.item_id
            WHERE (a.name LIKE '%$params[2]%'
            OR a.description LIKE '%$params[2]%'
            OR a.full_description LIKE '%$params[2]%') 
            AND a.status = 1
            GROUP BY a.item_id