如何使用Sequelize JS for MySQL编写查询

时间:2019-07-18 04:33:58

标签: javascript node.js sequelize.js

我想要使用sequelize js这样的查询

SELECT `PointOfSale`.`id`, `PointOfSale`.`title`, `PointOfSale`.`description`, `PointOfSale`.`code`, `PointOfSale`.`total_items`, `PointOfSale`.`ref_client`, `PointOfSale`.`payment_type`, `PointOfSale`.`total_received`, `PointOfSale`.`discount_in_cash`, `PointOfSale`.`discount_type`, `PointOfSale`.`discount_percent`, `PointOfSale`.`total_payable`, `PointOfSale`.`customer_pay`, `PointOfSale`.`customer_return`, `PointOfSale`.`status`, `PointOfSale`.`order_status`, `PointOfSale`.`createdAt`, `PointOfSale`.`updatedAt`, `OrderedItems`.`id` AS `OrderedItems.id`, `OrderedItems`.`item_price` AS `OrderedItems.item_price`, `OrderedItems`.`item_code` AS `OrderedItems.item_code`, `OrderedItems`.`item_id` AS `OrderedItems.item_id`, `OrderedItems`.`item_quantity` AS `OrderedItems.item_quantity`, `OrderedItems`.`order_code` AS `OrderedItems.order_code`, `OrderedItems`.`total_price` AS `OrderedItems.total_price`, `OrderedItems`.`status` AS `OrderedItems.status`, `OrderedItems`.`createdAt` AS `OrderedItems.createdAt`, `OrderedItems`.`updatedAt` AS `OrderedItems.updatedAt` FROM `point_of_sale` AS `PointOfSale` LEFT OUTER JOIN `ordered_items` AS `OrderedItems` ON `PointOfSale`.`code` = `OrderedItems`.`order_code` WHERE `PointOfSale`.`code` = 'ROMDS1' AND `PointOfSale`.`status` = 1 AND `PointOfSale`.`order_status` = 'pending';

但是我总是得到这个

SELECT `PointOfSale`.`id`, `PointOfSale`.`title`, `PointOfSale`.`description`, `PointOfSale`.`code`, `PointOfSale`.`total_items`, `PointOfSale`.`ref_client`, `PointOfSale`.`payment_type`, `PointOfSale`.`total_received`, `PointOfSale`.`discount_in_cash`, `PointOfSale`.`discount_type`, `PointOfSale`.`discount_percent`, `PointOfSale`.`total_payable`, `PointOfSale`.`customer_pay`, `PointOfSale`.`customer_return`, `PointOfSale`.`status`, `PointOfSale`.`order_status`, `PointOfSale`.`createdAt`, `PointOfSale`.`updatedAt`, `OrderedItems`.`id` AS `OrderedItems.id`, `OrderedItems`.`item_price` AS `OrderedItems.item_price`, `OrderedItems`.`item_code` AS `OrderedItems.item_code`, `OrderedItems`.`item_id` AS `OrderedItems.item_id`, `OrderedItems`.`item_quantity` AS `OrderedItems.item_quantity`, `OrderedItems`.`order_code` AS `OrderedItems.order_code`, `OrderedItems`.`total_price` AS `OrderedItems.total_price`, `OrderedItems`.`status` AS `OrderedItems.status`, `OrderedItems`.`createdAt` AS `OrderedItems.createdAt`, `OrderedItems`.`updatedAt` AS `OrderedItems.updatedAt` FROM `point_of_sale` AS `PointOfSale` LEFT OUTER JOIN `ordered_items` AS `OrderedItems` ON `PointOfSale`.`code` WHERE `PointOfSale`.`code` = 'ROMDS1' AND `PointOfSale`.`status` = 1 AND `PointOfSale`.`order_status` = 'pending'

这是我的代码

PointOfSale.hasMany(OrderedItems, {
        foreignKey: "order_code",
        targetKey: "order_code"
      });
      OrderedItems.hasMany(PointOfSale, {
        foreignKey: "code",
        targetKey: "code"
      });
      PointOfSale.findAll({
        // as: "PointOfSale",
        include: [
          {
            model: OrderedItems,
            all: true,
            as: "OrderedItems",
            on: {
              order_code: Sequelize.col("PointOfSale.code")
            }
          }
        ],
        where: {
          code: orderCode,
          status: 1,
          order_status: "pending"
        }
      })
        .then(res => {
          resolve(res);
        })
        .catch(e => {
          console.log(e);
          reject(e);
        });
    });

“ {” data“:[{” id“:115,” title“:null,” description“:”“,” code“:” ROMDS1“,” total_items“:2,” ref_client“:1, “ payment_type”:“待处理”,“收款总额”:3024,“ discount_in_cash”:0,“ discount_type”:“价格”,“ discount_percent”:0,“ total_payable”:3024,“ customer_pay”:0,“ customer_return”: 0,“ status”:true,“ order_status”:“ pending”,“ createdAt”:“ 2019-07-16T23:39:53.000Z”,“ updatedAt”:“ 2019-07-16T23:39:53.000Z”, “ OrderedItems”:[i_want_order_items_array]}]}“

0 个答案:

没有答案