偏移和限制语法错误

时间:2018-03-30 13:38:56

标签: sql mariadb

这里有什么问题

成功运行

update posts set pos = 'right' where pos = 'below' limit 4 offset 10;

我也尝试过:

update posts set pos = 'right' where pos = 'below' offset 10 limit 4;
update posts set pos = 'right' limit 4 offset 10;
update posts set pos = 'right' offset 10 limit 4;

总是一样的错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'offset 10' at line 1

3 个答案:

答案 0 :(得分:2)

假设您的posts表格有id主键,您可以:

update  posts 
set     pos = 'right' 
where   id in
        (
        select  id
        from    (
                select  id
                from    posts
                where   pos = 'below' 
                order by
                        id
                limit   4
                offset  10
                ) sub
        )

Example at DB Fiddle.双子查询需要解决MySQL的doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'错误。喜欢 Raymond Nijland评论说,MySQL通常以主键顺序返回行,但你不能在没有order by的情况下严格依赖行。

答案 1 :(得分:2)

就像Barmar在评论中所说,你不能在UPDATE查询中使用带有LIMIT的OFFSET。

替换的最佳方法

update posts set pos = 'right' where pos = 'below' limit 4 offset 10;

假设你有一个带有PRIMARY KEY和auto_increment选项的id列..这应该像使用limit 4 offset 10limit 10. 4

一样工作

p.s 请注意,id必须是11到14之间的增量,而不会删除id(s)。

update posts set pos = 'right' where pos = 'below' and (id > 10 and id <= 14)

p.s 此查询将在删除ID('s)时生效

update posts set pos = 'right' where pos = 'below' and id > 10 LIMIT 4

答案 2 :(得分:1)

LIMIT 可与更新一起使用,但仅行数

所以下面的查询将无限制地正常工作

    bool checkUserGroup()
    {
        List<GroupPrincipal> userGroups = new List<GroupPrincipal>();

        PrincipalContext currentDomain = new PrincipalContext(ContextType.Domain);

        UserPrincipal usr = UserPrincipal.FindByIdentity(currentDomain, "yourUserName");

        if (usr != null)
        {
            PrincipalSearchResult<Principal> foundGroups = usr.GetAuthorizationGroups();

            foreach (Principal p in foundGroups)
            {
                if (p is GroupPrincipal)
                {
                    userGroups.Add((GroupPrincipal)p);
                }
            }
        }

        return userGroups.Any(ro => ro.Name == "Administrators"); //Administrtor keyword can vary depending on the cultur.
    }

请查看以下链接,有相同的一些优点。

MySQL - UPDATE query with LIMIT

正如@Barmar建议的那样,请先检查一下syntex并搜索一下这个问题。 :)