在计数条件

时间:2017-09-07 12:36:30

标签: mysql sql-update

我有以下问题: 我希望更新COUNT条件大于1的所有行,否则我想要更新除1以外的所有行。它还应该按照other_ID更新。

Dummytable:

+----+----------+----------+-------------+
| id | other_ID | cirteria | updatefield |
+----+----------+----------+-------------+
|  1 |        1 |        1 |           0 |
|  2 |        1 |        1 |           0 |
|  3 |        1 |     1234 |           0 |
|  4 |        2 |        2 |           0 |
|  5 |        2 |        1 |           0 |
|  6 |        2 |        1 |           0 |
|  7 |        4 |       20 |           0 |
|  8 |        4 |        1 |           0 |
|  9 |        4 |       60 |           0 |
| 10 |        5 |        1 |           0 |
| 11 |        5 |        1 |           0 |
| 12 |        6 |        5 |           0 |
+----+----------+----------+-------------+

出人意料的结果:

+----+----------+----------+-------------+
| id | other_ID | cirteria | updatefield |
+----+----------+----------+-------------+
|  1 |        1 |        1 |           1 |
|  2 |        1 |        1 |           1 |
|  3 |        1 |     1234 |           0 |
|  4 |        2 |        2 |           0 |
|  5 |        2 |        1 |           1 |
|  6 |        2 |        1 |           1 |
|  7 |        4 |       20 |           0 |
|  8 |        4 |        1 |           1 |
|  9 |        4 |       60 |           0 |
| 10 |        5 |        1 |           0 |
| 11 |        5 |        1 |           1 |
| 12 |        6 |        5 |           0 |
+----+----------+----------+-------------+

我的想法:

UPDATE pics AS tu SET updatefield=1 WHERE criteria=1 AND (select count(*) as cnt2 from pics where criteria>1 group by other_id)>1;

错误:表'tu'被指定两次,既作为'UPDATE'的目标又作为数据的单独来源

我也有问题正确计算:     SELECT other_id,count()as cnt FROM pics AS ts WHERE criteria = 1 and(select count()as cnt2 from pics where criteria> 1)> 0 GROUP BY other_id;

我想得到cnt = 1 for other_id = 5,但我得到cnt = 2

与     SELECT other_id,COUNT(*)AS cnt2     从照片     WHERE标准> 1     GROUP BY other_id;

我得到了所有其他我要更新updatefield的地方。但是如何将其与更新连接?以及如何获得除other_id = 5之外的所有内容

2 个答案:

答案 0 :(得分:1)

您可以将子查询别名为另一个查询,例如:

UPDATE test
SET updatefield = 1
WHERE updatefield = 0 AND criteria = 1
AND other_id IN (
    SELECT a.id FROM (
        SELECT other_id AS id
        FROM test
        WHERE criteria > 1
        GROUP BY other_id
        HAVING COUNT(*) > 1
     ) a
  );

这是 SQL Fiddle

<强>更新

这将更新标准为0>1的记录的ID。现在,要更新具有1条件的多条记录的记录,您需要执行以下操作:

UPDATE test
SET updatefield = 1
WHERE updatefield = 0 AND criteria = 1
AND id IN (
    SELECT a.id FROM (
        SELECT MIN(id) AS id
        FROM test
        WHERE criteria = 1
        GROUP BY other_id
        HAVING COUNT(*) > 1
     ) a
  );

答案 1 :(得分:0)

感谢@Darshan Mehtas answer and help我终于找到了解决方案来解决它。

以下是完整的解决方案:

UPDATE test
SET updatefield = 1
WHERE updatefield = 0 AND criteria = 1
AND id not IN (
  SELECT a.id FROM (
    SELECT id
    FROM test
    WHERE criteria>1
  ) a
)
AND id not IN (
  SELECT b.id FROM (
    SELECT id
    FROM test
    GROUP BY other_id
    HAVING COUNT(*) = 1
  ) b
)
AND id NOT IN (
  SELECT c.id FROM (
    SELECT id
    FROM test
    WHERE criteria=1 AND other_id NOT IN (
      SELECT other_id FROM test WHERE Criteria>1
    )
    GROUP BY other_id, criteria
    HAVING COUNT(criteria)>1
  ) c
);

简短说明:

  1. 第一个子查询(a)过滤标准大于1的ID
  2. 第二个子查询(b)过滤仅包含结果
  3. 的ID
  4. 第三子查询(c)过滤ID如果标准为1an,则没有任何更高的标准,并保留,thansk分组,第一个结果。
  5. 唯一不好的事情可能是在最后一个子查询(c)中保留第一个(大部分是最旧的)结果而不是最新的结果。

    €二叔: 保持最后的结果使用它来代替子查询c:

    AND id NOT IN (
      SELECT c.id FROM (
        SELECT id
        FROM test t1
        JOIN (SELECT other_id, max(id) maxid
              FROM test
              GROUP BY other_id) t2
        ON t1.otheR_id=t2.other_id AND t1.id=t2.maxid
        WHERE criteria=1 AND t1.other_id NOT IN (
          SELECT other_id FROM test WHERE Criteria>1
        )
        GROUP BY t1.other_id, criteria
      ) c
    );