从MySQL的返回结果中排除一个值

时间:2014-11-06 16:10:00

标签: mysql

我遇到了问题,我找不到答案。我在java进程中查询MySql表,我想从查询返回中排除一些行。

以下是查询:

SELECT 
    o.offer_id,
    o.external_cat,
    o.cat,
    o.shop,
    o.item_id,
    oa.value
FROM
    offer AS o,
    offerattributes AS oa
WHERE
    o.offer_id = oa.offer_id
        AND (cat = 1200000 OR cat = 12050200
            OR cat = 13020304
            OR cat = 3041400
            OR cat = 3041402)
        AND (oa.attribute_id = 'status_live_unattached_pregen'
            OR oa.attribute_id = 'status_live_attached_pregen'
            OR oa.attribute_id = 'status_dead_offer_getter'
            OR oa.attribute_id = 'most_recent_status')
        AND (oa.value = 'OK'
            OR oa.value='status_live_unattached_pregen'
            OR oa.value='status_live_attached_pregen'
            OR oa.value='status_dead_offer_getter')

这里的诀窍是我需要将值设置为'OK'才能继续我的进程但是我不需要mysql在其响应中返回它,我只需要返回其他值,暂时它通过查询返回两行,一行带有“OK”值,另一行带有其他值之一。

我希望返回值如下:

'000005261383370', '10020578', '1200000', '562', '1000000_157795705', 'status_live_attached_pregen'

我的查询,但它返回:

'000005261383370', '10020578', '1200000', '562', '1000000_157795705', 'OK'
'000005261383370', '10020578', '1200000', '562', '1000000_157795705', 'status_live_attached_pregen'

真的很感激一些帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

你可以用我自己的INNER JOIN来解决这个问题:

SELECT o.offer_id
    ,o.external_cat
    ,o.cat
    ,o.shop
    ,o.item_id
    ,oa.value
FROM offer AS o
INNER JOIN offerattributes AS oa
    ON o.offer_id = oa.offer_id
INNER JOIN offerattributes AS oaOK
    ON oaOK.offer_id = oa.offer_id
        AND oaOK.value = 'OK'
WHERE o.cat IN (1200000,12050200,13020304,3041400,3041402)
    AND oa.attribute_id IN ('status_live_unattached_pregen','status_live_attached_pregen','status_dead_offer_getter','most_recent_status')
    AND oa.value IN ('status_live_unattached_pregen','status_live_attached_pregen','status_dead_offer_getter');

通过自我JOIN并限制值OK,它会将结果集限制为offer_id响应OK,但{ {1}}子句仍将检索您需要的值。根据您的描述,我认为这正是您所寻找的。

我还将您的隐式交叉WHERE转换为明确的JOIN,并将您的INNER JOIN更改为OR,这样做应该更高效。