使用MYSQL时,Grails条件查询失败,语法错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用MYSQL时,Grails条件查询失败,语法错误相关的知识,希望对你有一定的参考价值。

当应用程序连接到mysql实例时,我的查询失败。使用H2内存数据库时,查询工作正常。所有其他查询都可以使用MYSQL。

错误消息:您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在第1行的')'附近使用正确的语法

控制器动作:

def index() {
def currentUser = currentUser()
def peopleFollowing = currentUser.following
def c = Post.createCriteria()
    def postList = c.list  {
       'in' ("user", peopleFollowing)
       'order' "dateCreated", "desc"
    }
[postList:postList, user : currentUser, peopleFollowing:peopleFollowing]
}

当我删除in子句时,查询将针对MYSQL运行。所以似乎:'in'(“用户”,peopleFolllowing)导致了这个问题。我认为问题在于它是一个MYSQL保留字。我尝试过使用反引号,但一直都会出现语法错误......?

class Post {


String content
Date dateCreated
User user


static belongsTo = [user : User]

static hasMany = [postComments: PostComment]

static constraints = {
    content (blank: false)
}

static mapping = {
    sort dateCreated:"desc"
    content type:"text"
    postComments sort:"dateCreated",  order:"desc"
}

}

class Post {String content Date dateCreated User user static belongsTo = [user:User] static hasMany = [postComments:PostComment] static constraints = {content(blank:false)} static mapping = {sort dateCreated:“desc”content type:“ text“postComments sort:”dateCreated“,order:”desc“}} class Post {String content Date dateCreated User user static belongsTo = [user:User] static hasMany = [postComments:PostComment] static constraints = {content(blank:false) )} static mapping = {sort dateCreated:“desc”内容类型:“text”postComments sort:“dateCreated”,order:“desc”}}

答案

我通过这样做来解决问题:

def postList = Post.findAllByUserInList( peopleFollowing )
另一答案

我使用ids解决了它,并在0中添加了一个List

def currentUser = currentUser()
def peopleFollowingIds = currentUser.following.id
peopleFollowingIds.add(0)
def c = Post.createCriteria()
def postList = c.list  {
    'in' ("user_id", peopleFollowingIds)
    'order' "dateCreated", "desc"
}

或类似的东西。

以上是关于使用MYSQL时,Grails条件查询失败,语法错误的主要内容,如果未能解决你的问题,请参考以下文章

运行 Grails 3.3.8 应用程序启动失败时出错

Mysql中表名如果是3e开头,查询时报语法错,原因未知

Mysql中表名如果是3e开头,查询时报语法错,原因未知

使用 hasmany 字符串查询 Grails / GORM 条件

带有条件的 Grails 查询:如何取回带有列的地图?

grails连接数据库