Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用join语句删除查询_Sql_Sql Server - Fatal编程技术网

Sql 使用join语句删除查询

Sql 使用join语句删除查询,sql,sql-server,Sql,Sql Server,我想从制造商产品规格表中删除该记录。删除后是否需要指定制造商产品规格?是否有其他方法使用delete语句和join语句?以上是执行此操作的最佳方法还是唯一方法,还是可以修改我的联接以在更安全的过程中执行删除?您的查询看起来不错,使用联接是一种安全的方法。是的,当连接两个表时,需要在delete语句后定义一个表名 我只建议编写内部连接而不是连接。这是相同的,但它更清楚地表明您真正想要的是内部联接,而不是左联接或右联接 我倾向于在这种情况下使用exists: Delete mfg_product_

我想从制造商产品规格表中删除该记录。删除后是否需要指定制造商产品规格?是否有其他方法使用delete语句和join语句?以上是执行此操作的最佳方法还是唯一方法,还是可以修改我的联接以在更安全的过程中执行删除?

您的查询看起来不错,使用联接是一种安全的方法。是的,当连接两个表时,需要在delete语句后定义一个表名

我只建议编写内部连接而不是连接。这是相同的,但它更清楚地表明您真正想要的是内部联接,而不是左联接或右联接

我倾向于在这种情况下使用exists:

Delete  mfg_product_specification 
from mfg_product_specification
    inner join dbo.spec_attribute  on mfg_product_specification.attribute_id=spec_attribute.attribute_id 
where spec_attribute.attribute_name = 'MEMSEL_SLOTS_NUM'
    and mfg_product_specification.mfg_product_id in (247174) 
    and  mfg_product_specification.value=''
我发现逻辑更容易遵循:从满足以下条件的ps中删除记录


我只是不把JOIN和DELETE联系起来,这是因为JOIN在概念上创建了一个新结果,它本质上是笛卡尔积的一个子集,从原始表中删除。此外,EXISTS保证每一行只尝试删除一次。但是,如果联接不是1-1,则联接可以生成一行的多个副本。

请注意,删除时不需要select语句。我编辑了你的问题。
Delete  mfg_product_specification 
from mfg_product_specification
    inner join dbo.spec_attribute  on mfg_product_specification.attribute_id=spec_attribute.attribute_id 
where spec_attribute.attribute_name = 'MEMSEL_SLOTS_NUM'
    and mfg_product_specification.mfg_product_id in (247174) 
    and  mfg_product_specification.value=''
Delete  ps
from mfg_product_specification ps
where ps.mfg_product_id in (247174) and
      ps.value = '' and
      exists (select 1
              from dbo.spec_attribute sa
              where ps.attribute_id = sa.attribute_id and
                    sa.attribute_name = 'MEMSEL_SLOTS_NUM'
             );