删除某些列

时间:2016-07-23 00:19:12

标签: mysql sql database

这是我试图在MySQL中执行的查询:

DELETE from Connection 
where
(sourceIp , destinationIp,
destinationPort,
sourceServerId,
destinationServerId,
sourceProcessId,
destinationProcessId) IN (Select 
    sourceIp,
        destinationIp,
        destinationPort,
        sourceServerId,
        destinationServerId,
        sourceProcessId,
        destinationProcessId
from
    (Select 
        sourceIp,
            destinationIp,
            destinationPort,
            sourceServerId,
            destinationServerId,
            sourceProcessId,
            destinationProcessId
    from
        Connection

    where
        sourceIp = '12.43.34.53'
        and destinationIp = '12.43.34.65'
        and destinationPort = '3306'
        and ((sourceServerId = '1'
        and destinationServerId = '2'
        and sourceProcessId = '1'
        and destinationProcessId = '2')
        or (sourceServerId IS NULL
        and destinationServerId = '2'
        and destinationProcessId = '2')
        or (sourceServerId = '1'
        and sourceProcessId = '1'
        and destinationServerId is NULL))) as C);

我正在进行多次选择,因为我无法在Clause中指定相同的目标表。上面的查询执行。我的表看起来像这样:

  1. sourceIp,destinationIp,destinationPort,sourceServerId,destinationServerId,sourceProcessId,destinationProcessId ' 12.43.34.53',' 12.43.34.65',' 3306',NULL,' 2',NULL,' 2'
  2. sourceIp,destinationIp,destinationPort,sourceServerId,destinationServerId,sourceProcessId,destinationProcessId ' 12.43.34.53',' 12.43.34.65',' 3306',' 1',NULL,' 1&# 39;,NULL
  3. sourceIp,destinationIp,destinationPort,sourceServerId,destinationServerId,sourceProcessId,destinationProcessId ' 12.43.34.53',' 12.43.34.65',' 3306',' 1',' 2' ,' 1',' 2'
  4. 以上查询仅删除最后一行(#3)。但是如果在IN处剪切查询并将其余部分作为Select执行,我将获得所有3行。是因为带空值的IN不起作用吗?有关如何修改此查询以使其删除所有3行的任何建议。 (所有id和ips以及目标端口一起使表中的行唯一)

1 个答案:

答案 0 :(得分:1)

哇,你不需要任何这些选择,并且如果这些字段中的任何一个返回IN,那么使用NULL这个容量将不会给你想要的结果..... .....你的问题出在你的WHERE声明中。

上面的清理可以简化为:

DELETE from Connection 
where
    sourceIp = '12.43.34.53'
    and destinationIp = '12.43.34.65'
    and destinationPort = '3306'
    and ((sourceServerId = '1'
    and destinationServerId = '2'
    and sourceProcessId = '1'
    and destinationProcessId = '2')
    or (sourceServerId IS NULL
    and destinationServerId = '2'
    and destinationProcessId = '2')
    or (sourceServerId = '1'
    and sourceProcessId = '1'
    and destinationServerId is NULL))
;

您没有删除正确记录的原因问题是由于您在WHERE子句中的条件优先顺序。我不打算解开这个,因为你的帖子需要一些工作,比如示例数据和期望的结果,因为我确信会有更好的方法来编写你的查询。