我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Mysql 游标 获取order by limit 1 结果不是selec 出来的结果

短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Mysql 游标 获取order by limit 1 结果不是selec 出来的结果

Mysql 游标 获取order by limit 1 结果不是selec 出来的结果

SELECT location_no,
                lot_batch_no,
                qty_onhand,
                qty_reserved,
                id,
                receipt_date,
                product_date,
                expiry_date,
                pos_x
          FROM (
                 SELECT  s.location_no                 location_no, 
                         s.lot_batch_no                lot_batch_no,
                         s.qty_onhand                  qty_onhand, 
                         s.qty_reserved                qty_reserved,
                         s.id                          id,
                         s.receipt_date                receipt_date,
                         s.product_date                product_date,
                         s.expiry_date                 expiry_date,
                         m.pos_x                       pos_x
                 FROM    bay_list b, inventory_location m, inventory_part_in_stock s
                 WHERE   b.logistics_company_id = m.logistics_company_id
                 AND     b.bay_no = m.bay_no
                 AND     b.warehouse = m.warehouse
                 AND     m.lock_inventory = 0    -- 未锁定
                 AND     (b.bay_type = 1 OR 1 IS NULL) 
                 AND     m.location_no != "JHKW" -- JHKW为虚拟库位,不可以拣
                 AND     m.logistics_company_id = s.logistics_company_id
                 AND     m.warehouse = s.warehouse
                 AND     m.location_no = s.location_no
                 AND     s.qty_reserved >= 0
                 AND     s.qty_onhand - s.qty_reserved > 0
                 AND     s.part_no = "2047222"
                 AND     s.logistics_company_id = 10003
                 AND     s.warehouse = "GLP"
                 AND     s.owner_id = "CRM-YC"
                 AND     b.pick_type != 3             -- 拣货类型 (1. 零散拣货   ; 2.批量拣货   ; 3. 预包拣货 )
                 AND     s.lot_batch_no LIKE CONCAT("%", "@NRM@NRM", "%")  -- @contrl_state@qulity_state                
                   ) k
                 ORDER BY product_date
                 LIMIT 1)

做了order by 第一个出来的时 生产日期最旧的,

但是跑存储过程的时候select 出来的缺不是这样的存储过程里面

DELIMITER $$

USE `zita_zwy_wms`$$

DROP PROCEDURE IF EXISTS `InventoryPartInStockUtil_FindAndResvWithLot`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `InventoryPartInStockUtil_FindAndResvWithLot`(  
             OUT qty_to_resv_              INT,
             OUT location_no_              VARCHAR(30),
             OUT lot_batch_no_             VARCHAR(30),
             IN company_id_                INT, 
             IN warehouse_                 VARCHAR(20),
             IN owner_id_                  VARCHAR(20),
             IN part_no_                   VARCHAR(30),
             IN qty_remain_                INT,
             IN location_type_             INT,              -- 库位类型.1.拣货区,2.存储区, 3: 次品区; 4: 待上架; 5: 退货区; 6: 分拣区
             IN user_id_                   VARCHAR(40),
             IN activity_id_               INT,
             IN pick_type_                 INT,
             IN part_lot_batch_no_         VARCHAR(20))
BEGIN 
   DECLARE id_                   INT;
   DECLARE qty_onhand_           INT;
   DECLARE qty_reserved_         INT;
   DECLARE order_by_             VARCHAR(2000);
   DECLARE receipt_date_         DATETIME;
   DECLARE product_date_         DATETIME;
   DECLARE expiry_date_          DATETIME;
   DECLARE pos_x_                INT;
   
   IF ((nvl(activity_id_ , 0) = 0) OR (pick_type_ != 3)) THEN   -- 拣货类型(1汇总拣货,2一单一品,3活动单拣货,4边分边拣, 5逐单拣货)
      BEGIN 
         -- 非活动订单的商品预留
         DECLARE get_inv_stk CURSOR FOR 
         SELECT location_no,
                lot_batch_no,
                qty_onhand,
                qty_reserved,
                id,
                receipt_date,
                product_date,
                expiry_date,
                pos_x
          FROM (
                 SELECT  s.location_no                 location_no, 
                         s.lot_batch_no                lot_batch_no,
                         s.qty_onhand                  qty_onhand, 
                         s.qty_reserved                qty_reserved,
                         s.id                          id,
                         s.receipt_date                receipt_date,
                         s.product_date                product_date,
                         s.expiry_date                 expiry_date,
                         m.pos_x                       pos_x
                 FROM    bay_list b, inventory_location m, inventory_part_in_stock s
                 WHERE   b.logistics_company_id = m.logistics_company_id
                 AND     b.bay_no = m.bay_no
                 AND     b.warehouse = m.warehouse
                 AND     m.lock_inventory = 0    -- 未锁定
                 AND     m.location_no != "JHKW" -- JHKW为虚拟库位,不可以拣货
                 AND     (b.bay_type = location_type_ OR location_type_ IS NULL) 
                 AND     m.logistics_company_id = s.logistics_company_id
                 AND     m.warehouse = s.warehouse
                 AND     m.location_no = s.location_no
                 AND     s.qty_reserved >= 0
                 AND     s.qty_onhand - s.qty_reserved > 0
                 AND     s.part_no = part_no_
                 AND     s.logistics_company_id = company_id_
                 AND     s.warehouse = warehouse_
                 AND     s.owner_id = owner_id_
                 AND     b.pick_type != 3             -- 拣货类型 (1. 零散拣货   ; 2.批量拣货   ; 3. 预包拣货 )
                 AND     s.lot_batch_no LIKE CONCAT("%", part_lot_batch_no_, "%")  -- @contrl_state@qulity_state 
                 ) k
                 ORDER BY order_by_
                 LIMIT 1; 
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET id_ = NULL;    
         
         SET   order_by_   := CompanyOwnerPriotiryUtil_GetConfig(company_id_, owner_id_);
         OPEN  get_inv_stk;
         FETCH get_inv_stk INTO location_no_, lot_batch_no_, qty_onhand_, qty_reserved_, id_, receipt_date_, product_date_, expiry_date_, pos_x_;
         CLOSE get_inv_stk;
         
         SELECT location_no_, lot_batch_no_, qty_onhand_, qty_reserved_, id_, receipt_date_, product_date_, expiry_date_, pos_x_;
      END;
   ELSE
      BEGIN 
        -- 活动订单的商品预留
        DECLARE get_inv_stk CURSOR FOR 
                SELECT  s.location_no                 location_no, 
                        s.lot_batch_no                lot_batch_no,
                        s.qty_onhand                  qty_onhand, 
                        s.qty_reserved                qty_reserved,
                        s.id                          id
                FROM    inventory_location m, inventory_part_in_stock s , activity_policy_loca a
                WHERE   a.logistics_company_id = s.logistics_company_id
                AND     a.warehouse = s.warehouse
                AND     a.owner_id = s.owner_id
                AND     m.logistics_company_id = s.logistics_company_id
                AND     m.warehouse = s.warehouse
                AND     m.lock_inventory = 0    -- 未锁定
                AND     m.location_no != "JHKW" -- JHKW为虚拟库位,不可以拣货
                AND     a.activity_id = activity_id_
                AND     s.qty_onhand - s.qty_reserved > 0
                AND     s.part_no = part_no_
                AND     s.logistics_company_id = company_id_
                AND     s.warehouse = warehouse_
                AND     s.owner_id = owner_id_
                AND     s.location_no = a.location_no
                AND     s.lot_batch_no LIKE CONCAT("%", part_lot_batch_no_, "%")  -- @contrl_state@qulity_state 
                ORDER BY s.lot_batch_no
                LIMIT 1; 
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET id_ = NULL;  
        
        OPEN  get_inv_stk;
        FETCH get_inv_stk INTO location_no_, lot_batch_no_, qty_onhand_, qty_reserved_, id_;
        CLOSE get_inv_stk;
      END;
   END IF; 
   
   SET qty_to_resv_ := 0;
   IF id_ > 0 THEN
      -- 锁定记录
      CALL InventoryPartInStockUtil_LockById( id_,
                                              qty_onhand_,
                                              "InventoryPartInStockUtil_FindAndResvWithLot" );
      --   
      SET qty_to_resv_ := LEAST( qty_onhand_ - qty_reserved_ , qty_remain_ );
      
      UPDATE inventory_part_in_stock
             SET  qty_reserved = GREATEST(0, LEAST(qty_onhand_, qty_reserved_ + qty_to_resv_))
             WHERE id = id_;
   END IF;
END$$

DELIMITER ;

注意:

解决方案 1.

 把order by 放到最里面去,结果正确

改了后还是不行

把变量放到 declare 这个游标前就可以了

 

SET @order_by_ := "s.product_date";
 SELECT  s.location_no                 location_no, 
                         s.lot_batch_no                lot_batch_no,
                         s.qty_onhand                  qty_onhand, 
                         s.qty_reserved                qty_reserved,
                         s.id                          id,
                         s.receipt_date                receipt_date,
                         s.product_date                product_date,
                         s.expiry_date                 expiry_date,
                         m.pos_x                       pos_x
                 FROM    bay_list b, inventory_location m, inventory_part_in_stock s
                 WHERE   b.logistics_company_id = m.logistics_company_id
                 AND     b.bay_no = m.bay_no
                 AND     b.warehouse = m.warehouse
                 AND     m.lock_inventory = 0    -- 未锁定
                 AND     m.location_no != "JHKW" -- JHKW为虚拟库位,不可以拣货
                 AND     (b.bay_type = 1 OR 1 IS NULL) 
                 AND     m.logistics_company_id = s.logistics_company_id
                 AND     m.warehouse = s.warehouse
                 AND     m.location_no = s.location_no
                 AND     s.qty_reserved >= 0
                 AND     s.qty_onhand - s.qty_reserved > 0
                 AND     s.part_no = "2047222"
                 AND     s.logistics_company_id = 10003
                 AND     s.warehouse = "GLP"
                 AND     s.owner_id = "CRM-YC"
                 AND     b.pick_type != 3             -- 拣货类型 (1. 零散拣货   ; 2.批量拣货   ; 3. 预包拣货 )
                 AND     s.lot_batch_no LIKE CONCAT("%", "@NRM@NRM", "%")  -- @contrl_state@qulity_state 
                 ORDER BY @order_by_

变量不可以写到order by 里面

 

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

Mysql 游标 获取order by limit 1 结果不是selec 出来的结果

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

Mysql 游标 获取order by limit 1 结果不是selec 出来的结果

SELECT location_no, lot_batch_no, qty_onhand, qty_reserved, id,
Mysql 游标 获取order by limit 1 结果不是selec 出来的结果
2015-04-11

编程热搜

目录