为多个选择SQL中的某些值返回NULL

时间:2018-06-25 19:35:11

标签: php mysql

我正在使用PHP的网站上工作,并具有以下代码:

INSERT INTO items (producttype, firstcode, secondcode, brand, retailer, firstsize, secondsize)
          SELECT ?, productcodes.id, productcodes.id, brands.id, retailers.id, sizes.id, sizes.id
          FROM productcodes, brands, retailers, sizes
          WHERE LOWER(productcodes.code)=LOWER(?)
          AND LOWER(productcodes.code)=LOWER(?)
          AND LOWER(brands.name)=LOWER(?)
          AND LOWER(retailers.name)=LOWER(?)
          AND LOWER(sizes.id)=LOWER(?)
          AND LOWER(sizes.id)=LOWER(?)

但是当我有这样的输入

INSERT INTO items (producttype, firstcode, secondcode, brand, retailer, firstsize, secondsize)
          SELECT 3, productcodes.id, productcodes.id, brands.id, retailers.id, sizes.id, sizes.id
          FROM productcodes, brands, retailers, sizes
          WHERE LOWER(productcodes.code)=LOWER(NULL)
          AND LOWER(productcodes.code)=LOWER(NULL)
          AND LOWER(brands.name)=LOWER('Canada')
          AND LOWER(retailers.name)=LOWER('WE')
          AND LOWER(sizes.id)=LOWER('XL')
          AND LOWER(sizes.id)=LOWER(NULL)

即使大多数列都可以为空(在代码中对此进行了较早的检查。如果只有一个实际返回0行,则对所有列都返回空(空)。

1 个答案:

答案 0 :(得分:0)

感谢您的所有出色提示/建议/解释。

通过使用该查询,我设法使我的代码正常工作,这可能是一个巨大的黑客攻击,或者对于服务器来说确实很沉重,但是现在它可以正常工作。如果有人对此代码有任何改进的建议,请留言。

INSERT INTO items 
        (producttype, 
         firstcode, 
         secondcode, 
         brand, 
         retailer, 
         firstsize, 
         secondsize)
SELECT 3, 
   IFNULL((SELECT productcodes.id 
           FROM   productcodes 
           WHERE  LOWER(productcodes.code) = LOWER(NULL)), NULL), 
   IFNULL((SELECT productcodes.id 
           FROM   productcodes 
           WHERE  LOWER(productcodes.code) = LOWER(NULL)), NULL), 
   IFNULL((SELECT brands.id 
           FROM   brands 
           WHERE  LOWER(brands.name) = LOWER('Canada')), NULL), 
   IFNULL((SELECT retailers.id 
           FROM   retailers 
           WHERE  LOWER(retailers.name) = LOWER('WE')), NULL), 
   IFNULL((SELECT sizes.id 
           FROM   sizes 
           WHERE  LOWER(sizes.size) = LOWER('XL')), NULL), 
   IFNULL((SELECT sizes.id 
           FROM   sizes 
           WHERE  LOWER(sizes.size) = LOWER(NULL)), NULL)