我需要添加到这条SQL指令来过滤一些项目?

时间:2013-01-15 01:27:13

标签: mysql sql

我想要的是过滤(而非获取)出现在这个新表中的商品,我们将其用作黑名单(例如,对于我们不希望提供原产地Y的域X)。

TABLE:      black_list_deals

COLUMNS: id 
         domain_id (we save here an id from the table "domain")
         origin_offer (we save here a value from deal, the field deal_source_id)

到目前为止,我正在使用此指令,但现在我需要根据黑名单表添加新行为以过滤这些商品。

SELECT
    a.lat,
    a.lon,
    a.id,
    a.image,
    a.link,
    a.deal_source_id,
    a.date_posted,
    a.date_expires,
    a.tags_external,
    a.tags_internal,
    a.gender,
    a.price,
    a.discount,
    a.areas,
    a.hits,
    a.bias,
    a.collection_type,
    a.uniqueness,
    a.date_posted = current_date AS today,
    DATEDIFF( a.date_expires, current_date) AS days_remaining,
    a.collection_type,
    b.id AS area_id,
    b.area,
    a.image_thumb,
    c.source_img_email,
    c.source_price,
    c.source_img_sm,
    c.source_name,
    c.frame,
    a.date_posted = current_date AS today,
    d.country,
    d.region,
    e.level1,
    e.level2,
    e.level3,
    e.level4

FROM deal a
      LEFT JOIN area b ON (a.areas = b.id)
      LEFT JOIN deal_source c ON (a.deal_source_id = c.id)
      LEFT JOIN deal_travel d ON (a.id = d.deal_id)
      LEFT JOIN deal_travel_country e ON (d.country = e.id)
WHERE
      a.validated = 'y' AND
      a.date_posted = current_date AND
      a.date_expires >= current_date AND
      email_deal = '1'

2 个答案:

答案 0 :(得分:3)

在WHERE子句中添加WHERE a.deal_source_id NOT IN (SELECT origin_offer FROM black_list_deals)应解决此问题。

答案 1 :(得分:1)

我会添加

SELECT
   ...
FROM deal a
...
   LEFT JOIN black_list_deals bld ON (bld.origin_offer = a.deal_source_id)
...
WHERE
...
   AND bld.id IS NULL

基本上你只会返回找不到黑名单中的条目的数据