如果value为null,则忽略insert

时间:2015-07-10 06:01:32

标签: mysql

所以我有这样的查询:

INSERT INTO price_hist 
            ( 
                        date, 
                        product_id, 
                        price_min, 
                        price_max, 
                        price_avg,
                        merchants 
            ) 
            VALUES 
            ( 
                        '2015-07-10', 
                        100388, 
                        ( 
                               SELECT min(price) 
                               FROM   prices 
                               WHERE  product_id = 100388 
                               AND    active = 1 ), 
                        ( 
                               SELECT max(price) 
                               FROM   prices 
                               WHERE  product_id = 100388 
                               AND    active = 1 ), 
                        ( 
                               SELECT avg(price) 
                               FROM   prices
                               WHERE  product_id = 100388 
                               AND    active = 1 ), 
                        ( 
                               SELECT count(price) 
                               FROM   prices
                               WHERE  product_id = 100388 
                               AND    active = 1 ) 
            )

price_maxprice_minprice_avg不能为空,但并非所有产品都有价格,因此这些产品的子查询自然会返回NULL。如果我使用INSERT IGNORE,查询会执行,但不是跳过插入,而是将0值插入所有这些字段。无论如何,我可以修改它,所以如果任何字段为空,它会一起跳过插入?

product_history表已经设置了字段,因此它们不能为NULL。

3 个答案:

答案 0 :(得分:2)

只需使用import c.universe._ import Flag._ def expandType(typeTree: Tree) = { c.typecheck(q"type T = $typeTree") match { case x @ TypeDef(mods, name, tparams, rhs) => rhs.tpe } } 代替:

INSERT ... SELECT

答案 1 :(得分:0)

使用IFNULL()

试试这个

INSERT INTO price_hist 
        ( 
                    date, 
                    product_id, 
                    price_min, 
                    price_max, 
                    price_avg,
                    merchants 
        ) 
        VALUES 
        ( 
                    '2015-07-10', 
                    100388, 
                    ( 
                           IFNULL(SELECT min(price) 
                           FROM   prices 
                           WHERE  product_id = 100388 
                           AND    active = 1 ), 0), 
                    ( 
                           IFNULL(SELECT max(price) 
                           FROM   prices 
                           WHERE  product_id = 100388 
                           AND    active = 1 ), 0), 
                    ( 
                           IFNULL(SELECT avg(price) 
                           FROM   prices
                           WHERE  product_id = 100388 
                           AND    active = 1 ), 0), 
                    ( 
                           IFNULL(SELECT count(price) 
                           FROM   prices
                           WHERE  product_id = 100388 
                           AND    active = 1 ), 0) 
        )

答案 2 :(得分:0)

INSERT INTO price_hist 
            ( 
                        date, 
                        product_id, 
                        price_min, 
                        price_max, 
                        price_avg,
                        merchants 
            ) 
            VALUES 
            ( 
                        '2015-07-10', 
                        100388, 
                        ( 
                               SELECT ifnull(min(price),0)
                               FROM   prices 
                               WHERE  product_id = 100388 
                               AND    active = 1 ), 
                        ( 
                               SELECT max(price) 
                               FROM   prices 
                               WHERE  product_id = 100388 
                               AND    active = 1 ), 
                        ( 
                               SELECT ifnull(avg(price),0)
                               FROM   prices
                               WHERE  product_id = 100388 
                               AND    active = 1 ), 
                        ( 
                               SELECT count(price)
                               FROM   prices
                               WHERE  product_id = 100388 
                               AND    active = 1 ) 
            )