在 c# 和 mysql 中使用带有变量的查询

本文关键字:变量 查询 mysql | 更新日期: 2023-09-27 17:56:03

我需要执行以下一组语句,目前它给出了致命错误我需要以下查询的查询输出,该查询使用两个变量

数据库是MySQL,语言是C#我正在尝试在 c# 代码中使用它MyReader=new MySQLCommand(this query ,connection object).执行读取器()

SET @lastItem := 0, @lastValue := 0; 
SELECT CONCAT (
  DATE (t5.InventoryDate)
  ,t5.SKUorItem
  ) AS PK,
    t5.CustomerID, t5.entityID, t5.inventoryDate, t5.SKUorItem, t5.Category, t5.inventory FROM
(
 SELECT 
 '3' as CustomerID,
 '90' as entityID,
 t1.InventoryDate as inventoryDate, 
 t1.idItem as SKUorItem, 
 t4.categoryInventary as Category,
 ifnull(t1.itemQty,0) as sales, ifnull(t2.buyQty,0) as StockMove,
 @lastValue := if( @lastItem = t1.idItem, @lastValue + ifnull(t2.buyQty,0) - ifnull(t1.itemQty,0), ifnull(t2.buyQty,0) - ifnull(t1.itemQty,0) ) as inventory,
 @lastItem := t1.idItem
 FROM
 (
 select date(date) as inventorydate, idItem, sum(quantity) as itemqty 
 from subway.saleitem 
 group by idItem, date(date)
 ) as t1
 LEFT OUTER JOIN
 (
 select date(date) as InventoryDate, idItem, Sum(initialBuyQuantity) as buyqty
 FROM Subway.InvoiceStock
 GROUP BY idItem, date(date)
 ) as t2
 ON t1.InventoryDate = t2.InventoryDate and t1.idItem = t2.idItem
 INNER JOIN subway.Item as t3 on t1.iditem = t3.iditem
 LEFT JOIN subway.CategoryInventary as t4 on t3.idCategoryInventary = t4.idCategoryInventary
 order by t1.iditem, t1.InventoryDate
) t5

在 c# 和 mysql 中使用带有变量的查询

将这些查询包装到Stored ProcedureUDF中。只需从c#代码中调用它即可。