通过mysql在表中插入值

问题描述 投票:0回答:2

我想通过使用过程在表中插入值 我在产品表中有 2 个表 sales 和 products 我有 id,product_name,product_price, product_quantity 和销售表中我有 p_id 和 p_qty 当用户在销售表过程中添加值时使用过程 2 个参数 1 个用于 p_id,其他用于 p_qty 如果 p_qty 小于 0 或大于 product_quantity 比 showstock 不可用

如果以上陈述我想回答 当用户在销售表过程中添加值时使用过程 2 个参数 1 个用于 p_id,其他用于 p_qty 如果 p_qty 小于 0 或大于 product_quantity 比 showstock 不可用

enter image description here

mysql if-statement conditional-statements phpmyadmin insert
2个回答
0
投票

product_qty_proc > products.product_quantity
需要在查询中引用
products
表中的特定行。您可以使用子查询来选择
product_id_proc
的数量。将该条件替换为:

product_qty_proc > (
    SELECT product_quantity 
    FROM products 
    WHERE product_id = product_id_proc
)

另外,

product_qty_proc>0
应该是
product_qty_proc<0
.


0
投票

这是我的查询

DELIMITER //
CREATE PROCEDURE find_qty_(
    product_id_proc INT,
    product_qty_proc INT
)
BEGIN
    DECLARE
        remaining_stock INT; 
        SELECT product_quantity INTO remaining_stock FROM products
        WHERE id = product_id_proc; 
        IF product_qty_proc>=0 OR product_qty_proc>remaining_stock THEN 
            BEGIN SELECT "stock not available";
            END;
        ELSE 
            INSERT into sales(p_id,p_qty)VALUES(product_id_proc,product_qty_proc);
        END IF; 
    END;;
© www.soinside.com 2019 - 2024. All rights reserved.