从表a中获取所有记录,其中b中id=somvalue加上b中没有id的记录

gk7wooem  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(302)

我每天有两张table coupons 表和a coupon_city_map table。
IDU代码1供应商202供应商103供应商504供应商405供应商90
优惠券\idu city \id12233442
我需要有身份证的优惠券 1 4, and 5 为了 city_id = 2 .
所以它应该把所有的优惠券拿到哪里去 city_id=2 i、 e.带id的优惠券 1 and 4 它还应该获取没有钥匙的优惠券 coupon_city_map5 .
这是我尝试过的,但问题仍然存在 [Op.or] 不起作用,它将返回所有优惠券。

let coupons = await Coupon.findAll({
      where: {
        [Op.or]: [
          { '$CouponCities.city_id$': city_id },
          { '$CouponCities.coupon_id$': null },
        ],
        ...filters // other filter like is_active: true
      },
      include: {
        model: CouponCity,
        attributes: [],
      },
      attributes: ['id', 'coupon_code', 'discount_per', 'flat_discount', 'discount_upto', 'description', 'display'],
    });

正在生成的查询

SELECT `Coupon`.`id`, 
       `Coupon`.`coupon_code`, 
       `Coupon`.`discount_per`, 
       `Coupon`.`flat_discount`, 
       `Coupon`.`discount_upto`, 
       `Coupon`.`description`, 
       `Coupon`.`display` 
FROM `coupons` AS `Coupon` 
LEFT OUTER JOIN `coupon_city_map` AS `CouponCities` ON `Coupon`.`id` = `CouponCities`.`coupon_id` 
WHERE (`Coupon`.`user_id` IS NULL OR `Coupon`.`user_id` = 1) 
  AND `Coupon`.`is_active` = true 
  AND `Coupon`.`is_external` = false 
  AND `Coupon`.`start_date` < '2020-12-30 10:33:20' 
  AND `Coupon`.`expiry_date` > '2020-12-30 10:33:20';

更新我也尝试下面,但它仍然返回所有的优惠券。

let coupons = await Coupon.findAll({
    // where: {
    //   ...filters,
    // },
    include: {
      model: CouponCity,
      required: false,
      where: {
        [Op.or]: [
          {
            zone_id: zoneId,
          }, {
            coupon_id: null,
          },
        ],
      },
      attributes: [],
    },
    attributes: ['id', 'coupon_code', 'discount_per', 'flat_discount','discount_upto', 'description', 'display'],
 });

…并生成以下查询。

SELECT `Coupon`.`id`, 
       `Coupon`.`coupon_code`, 
       `Coupon`.`discount_per`, 
       `Coupon`.`flat_discount`, 
       `Coupon`.`discount_upto`, 
       `Coupon`.`description`, 
       `Coupon`.`display` 
FROM   `coupons` AS `Coupon` 
       LEFT OUTER JOIN `coupon_city_map` AS `CouponCities` 
                    ON `Coupon`.`id` = `CouponCities`.`coupon_id` 
                       AND ( `CouponCities`.`zone_id` = 1 
                             AND `CouponCities`.`coupon_id` IS NULL ) 
WHERE  `Coupon`.`is_active` = true 
       AND `Coupon`.`is_external` = false;
vi4fp9gy

vi4fp9gy1#

使用并集
您可以编写如下查询

SELECT coupons.* 
FROM   coupons, 
       coupon_city_map 
WHERE  coupons.id = coupon_city_map.coupon_id 
       AND coupon_city_map.city_id = 2 
UNION 
SELECT coupons.* 
FROM   coupons 
WHERE  coupons.id NOT IN(SELECT coupon_city_map.coupon_id 
                         FROM   coupon_city_map)
jm81lzqq

jm81lzqq2#

这对我来说很有效,查询很混乱,但它是有效的。我张贴所有的代码,以便更好地了解任何感兴趣的人。
在这里 zone 是城市。

{
  const filters = {
    start_date: {
      [Op.lt]: new Date(),
    },
    expiry_date: {
      [Op.gt]: new Date(),
    },
  };
  if (userId) {
    filters[Op.or] = [{
      user_id: null,
    }, {
      user_id: userId,
    }];
    filters[Op.and] = {
      [Op.or]: [
        sequelize.literal('CouponZones.zone_id = 1'),
        sequelize.literal('CouponZones.coupon_id IS  null')
      ],
    };
  } else {
    filters.user_id = null;
  }
  let coupons = await Coupon.findAll({
    where: {
      ...filters,
    },
    include: {
      model: CouponZone,
      attributes: [],
    },
    attributes: ['id', 'coupon_code', 'discount_per', 'flat_discount', 'discount_upto', 'description', 'display'],
  });

这是它生成的查询。

SELECT `Coupon`.`id`, 
       `Coupon`.`coupon_code`, 
       `Coupon`.`discount_per`, 
       `Coupon`.`flat_discount`, 
       `Coupon`.`discount_upto`, 
       `Coupon`.`description`, 
       `Coupon`.`display` 
FROM   `coupons` AS `Coupon` 
       LEFT OUTER JOIN `coupon_zone_map` AS `CouponZones` 
                    ON `Coupon`.`id` = `CouponZones`.`coupon_id` 
WHERE  ( `Coupon`.`user_id` IS NULL 
          OR `Coupon`.`user_id` = 1 ) 
       AND ((CouponZones.zone_id = 1 OR CouponZones.coupon_id IS  null)) 
       AND `Coupon`.`is_active` = true 
       AND `Coupon`.`is_external` = false;

相关问题