Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails criteria或HQL for MySQL查询,带有左连接和日期函数_Mysql_Grails_Hql - Fatal编程技术网

Grails criteria或HQL for MySQL查询,带有左连接和日期函数

Grails criteria或HQL for MySQL查询,带有左连接和日期函数,mysql,grails,hql,Mysql,Grails,Hql,我有一个MySQL数据库,其中存储了某些商品的销售额。我想比较一下过去两天的销售情况,例如过去10天和过去9天。我设计了MySQL查询来实现这一点: select s1.sales - s2.sales from ProductSales as s1 left join ProductSales as s2 on s2.date >= DATE(DATE_ADD(Now(), INTERVAL '-10' DAY)) and s2.date <= DATE(DATE_ADD(Now(

我有一个MySQL数据库,其中存储了某些商品的销售额。我想比较一下过去两天的销售情况,例如过去10天和过去9天。我设计了MySQL查询来实现这一点:

select s1.sales - s2.sales from ProductSales as s1
left join ProductSales as s2 on s2.date >= DATE(DATE_ADD(Now(), INTERVAL '-10' DAY)) and s2.date <= DATE(DATE_ADD(Now(), INTERVAL '-9' DAY)) and s2.product_id = s1.product_id
where s1.date >= DATE(DATE_ADD(Now(), INTERVAL '-9' DAY));
从ProductSales中选择s1.sales-s2.sales作为s1
在s2.date>=日期(日期添加(现在(),间隔'-10'天))和s2.date=日期(日期添加(现在(),间隔'-9'天)),以s2.date作为s2加入ProductSales;
我希望在Grails应用程序中以编程方式执行此操作,并且需要将此查询转换为Criteria或HQL查询。我该怎么做

更新:我这样试过:

def date1 = new Date() - 2
def date2 = new Date() - 1
def sales = ProductSales.executeQuery( """
    select s1.sales - s2.sales from ProductSales s1
    left join ProductSales s2
    where s1.date >= :date2
    and s2.date >= :date1 and s2.date < :date2 and s2.product_id = s1.product_id""",
[ date1: date1, date2: date2 ] )
def date1=新日期()-2
def date2=新日期()-1
def sales=ProductSales.executeQuery(“”)
从ProductSales s1中选择s1.sales-s2.sales
左键连接ProductSales s2
其中s1.date>=:date2
和s2.date>=:date1和s2.date<:date2和s2.product\u id=s1.product\u id”“,
[日期1:date1,日期2:date2])
不幸的是,这给了我:

ERROR hql.ast.ErrorCounter -  Path expected for join!
ERROR hql.ast.ErrorCounter -  Invalid path: 's2.sales'
ERROR hql.ast.ErrorCounter -  right-hand operand of a binary operator was null
ERROR hql.ast.ErrorCounter -  Invalid path: 's2.date'
ERROR hql.ast.ErrorCounter - <AST>:0:0: unexpected end of subtree
ERROR hql.ast.ErrorCounter -  left-hand operand of a binary operator was null
ERROR hql.ast.ErrorCounter -  Invalid path: 's2.date'
ERROR hql.ast.ErrorCounter - <AST>:0:0: unexpected end of subtree
ERROR hql.ast.ErrorCounter -  left-hand operand of a binary operator was null
ERROR hql.ast.ErrorCounter -  Invalid path: 's2.product_id'
ERROR hql.ast.ErrorCounter - <AST>:0:0: unexpected end of subtree
ERROR hql.ast.ErrorCounter -  left-hand operand of a binary operator was null
ERROR logging.impl.SLF4JLog - QuerySyntaxException occurred when processing request: [GET] /app/dashboard
Path expected for join!
ERROR hql.ast.ErrorCounter-预期加入的路径!
错误hql.ast.ErrorCounter-无效路径:“s2.sales”
错误hql.ast.ErrorCounter-二进制运算符的右侧操作数为空
错误hql.ast.ErrorCounter-无效路径:“s2.date”
错误hql.ast.ErrorCounter-:0:0:子树意外结束
错误hql.ast.ErrorCounter-二进制运算符的左侧操作数为空
错误hql.ast.ErrorCounter-无效路径:“s2.date”
错误hql.ast.ErrorCounter-:0:0:子树意外结束
错误hql.ast.ErrorCounter-二进制运算符的左侧操作数为空
错误hql.ast.ErrorCounter-无效路径:“s2.product\u id”
错误hql.ast.ErrorCounter-:0:0:子树意外结束
错误hql.ast.ErrorCounter-二进制运算符的左侧操作数为空
logging.impl.SLF4JLog出错-处理请求[GET]/app/dashboard时发生QuerySyntaxException
预期加入的路径!
更新2:由于我无法回答自己的问题,我在这里添加了我的回答:


显然,这是应该的:

def now = new Date()
def date1 = now - 2
def date2 = now - 1
def sales = ProductSales.executeQuery( """
    select s1.owner, s1.sales - s2.sales from ProductSales s1, ProductSales s2
    where s1.date >= :date2 and s1.date < :now 
    and s2.date >= :date1 and s2.date < :date2 and s2.product = s1.product""",
    [ date1: date1, date2: date2, now: now ]
    )
def now = new Date()
def date1 = now - 2
def date2 = now - 1
def sales = ProductSales.executeQuery( """
    select s1.owner, s1.sales - s2.sales from ProductSales s1, ProductSales s2
    where s1.date >= :date2 and s1.date < :now 
    and s2.date >= :date1 and s2.date < :date2 and s2.product = s1.product""",
    [ date1: date1, date2: date2, now: now ]
    )
def now=新日期()
def date1=now-2
def date2=now-1
def sales=ProductSales.executeQuery(“”)
从ProductSales s1、ProductSales s2中选择s1.owner、s1.sales-s2.sales
其中s1.date>=:date2和s1.date<:now
和s2.date>=:date1和s2.date<:date2和s2.product=s1.product“”,
[日期1:date1,日期2:date2,now:now]
)

不过,我不完全确定
左连接和
从T1、T2选择之间有什么区别。从那以后,我为最近的条目添加了一个上限(
now
),并将
product\u id
更改为
product
,显然这是应该的:

def now = new Date()
def date1 = now - 2
def date2 = now - 1
def sales = ProductSales.executeQuery( """
    select s1.owner, s1.sales - s2.sales from ProductSales s1, ProductSales s2
    where s1.date >= :date2 and s1.date < :now 
    and s2.date >= :date1 and s2.date < :date2 and s2.product = s1.product""",
    [ date1: date1, date2: date2, now: now ]
    )
def now = new Date()
def date1 = now - 2
def date2 = now - 1
def sales = ProductSales.executeQuery( """
    select s1.owner, s1.sales - s2.sales from ProductSales s1, ProductSales s2
    where s1.date >= :date2 and s1.date < :now 
    and s2.date >= :date1 and s2.date < :date2 and s2.product = s1.product""",
    [ date1: date1, date2: date2, now: now ]
    )
def now=新日期()
def date1=now-2
def date2=now-1
def sales=ProductSales.executeQuery(“”)
从ProductSales s1、ProductSales s2中选择s1.owner、s1.sales-s2.sales
其中s1.date>=:date2和s1.date<:now
和s2.date>=:date1和s2.date<:date2和s2.product=s1.product“”,
[日期1:date1,日期2:date2,now:now]
)
不过,我不完全确定
左连接和
从T1、T2选择之间有什么区别。从那以后,我为最近的条目添加了一个上限(
now
),并将
product\u id
更改为
product