用合并语句替换当前表

时间:2019-03-01 15:37:08

标签: sql mysql-workbench

我有一个名为innerjoined的表:

enter image description here

此当前表的字段为空。而且我有一个代码可以删除具有空白值的行。

`SELECT productcode, Brand, product, size FROM innerjoined WHERE
 COALESCE(productcode, '') <> '' AND
 COALESCE(Brand, '') <> '' AND
 COALESCE(product, '') <> '' AND
 COALESCE(size, '') <> ''`

所以最终结果如下:

enter image description here

我的问题是: 如何在不基于此公式创建新表的情况下更新当前表? 我尝试了以下方法:

`update innerjoined(SELECT productcode, Brand, product, size FROM innerjoined WHERE
COALESCE(productcode, '') <> '' AND
COALESCE(Brand, '') <> '' AND
COALESCE(product, '') <> '' AND
COALESCE(size, '') <> '')`

1 个答案:

答案 0 :(得分:2)

首先,我只是不明白为什么您不使用:

SELECT productcode, Brand, product, size
FROM innerjoined
WHERE productcode <> '' AND
      Brand <> '' AND
      product <> '' AND
      size <> '';

它更简单,并且执行相同的操作。

第二,您似乎想要delete

delete i from innerjoined i
    where productcode = '' or productcode is null or
          brand = '' or brand is null or
          product = '' or product is null or
          size = '' or size is null;

Here是一个db <>小提琴,它说明所有这些代码都可以使用,同时包含空字符串和NULL。