使用子查询时出现语法错误

时间:2015-11-02 11:04:09

标签: mysql

以下是我的SQL查询

SELECT parts.product_code AS PC,SUM( x.goods_qty ) AS Qty, SUM( x.price * x.goods_qty ) / SUM( x.goods_qty ) AS Avg_Cost, parts.cost_price AS Avg_Cost1, parts.cost_price * parts.bulk_qty AS Total_Cost_Price1, SUM( x.price * x.goods_qty ) / SUM( x.goods_qty ) * parts.bulk_qty AS Total_Cost_Price, parts.sales_price * parts.bulk_qty AS Total_Sales_Price
FROM parts
LEFT JOIN received_goods_details x ON x.part_id = parts.id
LEFT JOIN invoice_details ON invoice_details.part_id = parts.id
WHERE parts.bulk_qty >0

AND
id IN

(SELECT * 
FROM  `received_goods_details` 
WHERE part_id = parts.id
ORDER BY id DESC 
LIMIT 5)


GROUP BY parts.id
LIMIT 0 , 30

I am getting a error  #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

我编辑了我的sql查询,删除了一个where子句

我试过这样但是它也没有用。在这里,我尝试在select语句本身中使用子查询。

SELECT parts.product_code AS PC, parts.part_no AS PO, parts.id AS part_id, parts.part_name AS Part_Name, parts.bulk_qty AS Stock, parts.cost_price AS LPP, parts.sales_price AS Sales_Price, SUM( x.goods_qty ) AS Qty, SUM( x.price * x.goods_qty ) / SUM( x.goods_qty ) AS Avg_Cost, parts.cost_price AS Avg_Cost1 FROM ( SELECT *
FROM received_goods_details 
WHERE  part_id = 8
ORDER BY id DESC 
LIMIT 5 ) ,

parts.cost_price * parts.bulk_qty AS Total_Cost_Price1, SUM( x.price * x.goods_qty ) / SUM( x.goods_qty ) * parts.bulk_qty AS Total_Cost_Price, parts.sales_price * parts.bulk_qty AS Total_Sales_Price
FROM parts
LEFT JOIN received_goods_details x ON x.part_id = parts.id
LEFT JOIN invoice_details ON invoice_details.part_id = parts.id
WHERE parts.bulk_qty >0
GROUP BY parts.id
LIMIT 0 , 30

现在我收到错误,如果

SELECT parts.product_code AS PC,parts.part_no AS PO,parts.id AS part_id,parts.part_name AS Part_Name,parts.bulk_qty AS Stock,parts.cost_price AS LPP,parts.sales_price AS Sales_Price,SUM(x。 goods_qty)AS数量,SUM(x.price * x.goods_qty)/ SUM(x.goods_qty)AS Avg_Cost,parts.cost_price AS Avg_Cost1 FROM(SELECT * 来自received_goods_details 在哪里y.part_id = parts.id ORDER BY id DESC 限制5)AS T,

1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以便在'* parts.bulk_qty AS Total_Cost_Price1,SUM(x.price * x.goods_qty)/ SUM(第7行的x.goo'

附近)使用正确的语法

1 个答案:

答案 0 :(得分:0)

SELECT parts.product_code AS PC,SUM( x.goods_qty ) AS Qty, SUM( x.price * x.goods_qty ) / SUM( x.goods_qty ) AS Avg_Cost, parts.cost_price AS Avg_Cost1, parts.cost_price * parts.bulk_qty AS Total_Cost_Price1, SUM( x.price * x.goods_qty ) / SUM( x.goods_qty ) * parts.bulk_qty AS Total_Cost_Price, parts.sales_price * parts.bulk_qty AS Total_Sales_Price
FROM parts
LEFT JOIN received_goods_details x ON x.part_id = parts.id
LEFT JOIN invoice_details ON invoice_details.part_id = parts.id
WHERE parts.bulk_qty >0    AND id IN
(SELECT * 
FROM  `received_goods_details` 
WHERE part_id = parts.id
ORDER BY id DESC 
LIMIT 5) GROUP BY parts.id  LIMIT 0 , 30

第二次删除。