数据库
首页 > 数据库> > 算在mysql更新mysql查询,错误支持

算在mysql更新mysql查询,错误支持

作者:互联网

你好需要从…开始更新

 SELECT 
     tA.id, count(*) c
FROM
    tA
        join
    tA ON tB.id = tA.id
where
    tA.id = 5
GROUP BY tA.id
having c > 1;

我有一对一的关系模型.但需要包括更新中的计数

   UPDATE tA
        join
    tA ON tB.id = tA.id 
set 
    cnt = 5;

假设这是计数的结果

其中’c’是第一个选择计数……可能是.

UPDATE tA
        join
    tA ON tB.id = tA.id 
set 
    cnt = (SELECT 
            count(*) c
        FROM
            tA
                join
            tA ON tB.id = tA.id
        WHERE
            tA.id = 5
        GROUP BY tA.id
        having c > 1);

说查询语法也不支持.. thnk

解决方法:

UPDATE  tA
        INNER JOIN
        (
            SELECT  tA.id, COUNT(*) totalCount
            FROM    tA INNER JOIN tB ON tB.id = tA.id
            WHERE   tA.id = 5
            GROUP   BY tA.id
            HAVING  COUNT(*) > 1
        ) b ON tA.id = b.id
SET     tA.cnt = b.totalCount

标签:mysql,sql,sql-update,jointable
来源: https://codeday.me/bug/20190709/1409830.html