mysql php查询错误,PHP mysqli查询错误,但查询正确

I have a MySQL Query which executes correctly in SequelPro but doesnt execute in PHP Mysqli. The message which is shown:

Execution stopped.

Message: An error occured when executing sql-statement: SET @csum := 0; select kunde, sales, (@csum := @csum + cr.sales) as cumulative_sales FROM (and the whole query (too long for printing it here)

I tried it a few times in Sequel PRo and it works.

and the query is stored in the variable $toBeExecuted

also printed it out to check if its correct and it is.

return mysqli_query($this->connectionTargetDB, $toBoExecuted);

I guess theres a problem with SET @csum := 0

/edit

The whole Query:

SET @csum := 0;

select kunde, sales, (@csum := @csum + cr.sales) as cumulative_sales

FROM (

SELECT j.kunde as kunde,

ROUND(SUM(m.ausgangsrechnungen - m.eingangsrechnungen), 2) as sales

FROM jobs_per_month m,

jobs j,

temporal_dates t

WHERE day(t._date) = 1

AND (t._date BETWEEN date_add(now(), INTERVAL -12 MONTH) and now())

AND m.monat = month(t._date)

AND m.jahr = year(t._date)

AND j.internal_jobnr = m.internal_jobnr

GROUP BY j.kunde

HAVING sales >= 10000

UNION ALL

SELECT concat(COUNT(r.sales), ' Kunde < 10k') as kunde,

ROUND(SUM(r.sales), 2) as sales

FROM (SELECT j.kunde as kunde,

ROUND(SUM(m.ausgangsrechnungen - m.eingangsrechnungen), 2) as sales

FROM jobs_per_month m,

jobs j,

temporal_dates t

WHERE day(t._date) = 1

AND (t._date BETWEEN date_add(now(), INTERVAL -12 MONTH) and now())

AND m.monat = month(t._date)

AND m.jahr = year(t._date)

AND j.internal_jobnr = m.internal_jobnr

GROUP BY j.kunde

HAVING sales < 10000 AND sales > 0) r

ORDER by sales desc) cr

解决方案

Figured the answer out myself.

The problem was as I said in SET @csum := 0;

I dont know why but it works when I do a CROSS JOIN (SELECT @csum:=1) c

after the last FROM in the statement.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值