我在mysql查询中收到错误?

[英]i am getting error in mysql query?


hi i have frame this query and i encountered a error like this.i have tried many but no result .i have modified query then its showing another error.
Failed to prepare the following query
Error preparing SQL statement.

嗨,我有框架这个查询,我遇到了像这样的错误。我尝试了很多但没有结果.i已经修改了查询然后显示另一个错误。准备以下查询失败准备SQL语句时出错。

SQL error #1:Syntax error: Encountered "MONTH" at line 4, column 46.





SELECT distinct productline as StringFact,orderdate,
SUM(CASE WHEN DATE(orderdate) between 
(DATE_FORMAT(DATE_ADD(orderdate,INTERVAL -12 MONTH),'%Y-m%-01'))
AND (DATE_FORMAT(DATE_ADD(orderdate,INTERVAL -11 MONTH),'%Y-m%-01')) THEN BUY END)          AS      PREVYEAR,

SUM(CASE WHEN DATE(orderdate) between 
 (DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL 0 MONTH),'%Y-m%-01'))
 AND (DATE_FORMAT(DATE_ADD(orderdate,INTERVAL 0 MONTH),'%Y-m%-01')) THEN BUY END) AS        CURRENTMONTH,

SUM(CASE WHEN DATE(orderdate) between 
(DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL -1 MONTH),'%Y-m%-01'))
AND (DATE_FORMAT(DATE_ADD(orderdate,INTERVAL -1 MONTH),'%Y-m%-01')) THEN BUY END) AS    LASTMONTH

  from (select orderdate,productline as StringFact,
  sum(QUANTITYINSTOCK + BUYPRICE) as BUY,
  sum(QUANTITYORDERED * PRICEEACH) as MSRP

   from customers, orders, orderdetails, products
  where customers.customernumber = orders.customernumber
   and orders.ordernumber = orderdetails.ordernumber
  and orderdetails.productcode = products.productcode
  Group By productline,orderdate
 )
 Group By productline,orderdate

1 个解决方案

#1


0  

The error message is not what i think it should be.

错误消息不是我认为它应该是。

What is your mysql version ?

你的mysql版本是什么?

1) You should give an alias to sub-query 2) Another problem is, you selected "product_line" at outer query, but product_line is selected as "StringFact" at sub-query, that's why the outer_query will not be able to select product_line.

1)您应该为子查询提供别名2)另一个问题是,您在外部查询中选择了“product_line”,但在子查询中选择了product_line作为“StringFact”,这就是为什么outer_query将无法选择product_line 。

I changed your query as follows and it executes with no problem.

我按如下方式更改了您的查询,它执行没有问题。

Here is the sqlfiddle for your query http://www.sqlfiddle.com/#!2/26218/6

这是您的查询http://www.sqlfiddle.com/#!2/26218/6的sqlfiddle

SELECT distinct productline as StringFact,orderdate,
SUM(CASE WHEN DATE(orderdate) between 
(DATE_FORMAT(DATE_ADD(orderdate,INTERVAL -12 MONTH),'%Y-m%-01'))
AND (DATE_FORMAT(DATE_ADD(orderdate,INTERVAL -11 MONTH),'%Y-m%-01')) THEN BUY END)              AS      PREVYEAR,

SUM(CASE WHEN DATE(orderdate) between 
 (DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL 0 MONTH),'%Y-m%-01'))
 AND (DATE_FORMAT(DATE_ADD(orderdate,INTERVAL 0 MONTH),'%Y-m%-01')) THEN BUY END) AS            CURRENTMONTH,

SUM(CASE WHEN DATE(orderdate) between 
(DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL -1 MONTH),'%Y-m%-01'))
AND (DATE_FORMAT(DATE_ADD(orderdate,INTERVAL -1 MONTH),'%Y-m%-01')) THEN BUY END) AS    LASTMONTH

  from (select orderdate,productline , # removed as StringFact
  sum(QUANTITYINSTOCK + BUYPRICE) as BUY,
  sum(QUANTITYORDERED * PRICEEACH) as MSRP

   from customers, orders, orderdetails, products
  where customers.customernumber = orders.customernumber
   and orders.ordernumber = orderdetails.ordernumber
  and orderdetails.productcode = products.productcode
  Group By productline,orderdate
 ) t # You need to give an alias
 Group By productline,orderdate

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/08/19/d34715ae9aa0e46e1e574c38fffaf720.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告