使用MYSQL时,Grails条件查询失败并出现synatx错误

时间:2015-12-09 00:26:57

标签: 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 {     字符串内容     日期dateCreated     用户用户     static belongsTo = [用户:用户]     static hasMany = [postComments:PostComment]     静态约束= {         内容(空白:假)     }     静态映射= {         sort dateCreated:“desc”         内容类型:“文字”         postComments排序:“dateCreated”,顺序:“desc”     } }     class Post {     字符串内容     日期dateCreated     用户用户     static belongsTo = [用户:用户]     static hasMany = [postComments:PostComment]     静态约束= {         内容(空白:假)     }     静态映射= {         sort dateCreated:“desc”         内容类型:“文字”         postComments排序:“dateCreated”,顺序:“desc”     }     }

2 个答案:

答案 0 :(得分:0)

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

def postList = Post.findAllByUserInList( peopleFollowing )

答案 1 :(得分:0)

我使用id s并将其添加到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"
}

或者类似的东西。