1

所以我想要做的是使用 C# 将累积数据放在 ListView 对象上,然后获取销售报告的销售记录。如您所见,它非常不稳定,我的代码就是,我主要想删除必须调用两个 MySqlCommands 和 DataReaders。任何建议将不胜感激!

string query = "SELECT * FROM orderlist ol "+
               "INNER JOIN orderdb o "+
               "ON ol.order_ID = o.order_ID "+
               "INNER JOIN menudb m "+
               "ON ol.menu_ID = m.menu_ID "+   
               "WHERE o.order_date >= '"+fromdate.Date()+"' AND "+
                     "o.order_date <= '"+todate.Date()+"'";
command = new MySqlCommand(query, connection);
dr = command.ExecuteReader();
string[] info = new string[15];
while(dr.Read())
{
  string query2 = "SELECT r.*, s.stock_pricePerPiece FROM recipelist r "+
                  "INNER JOIN stocksdb s ON r.stock_ID = s.stock_ID "+
                  "WHERE r.menu_ID = '"+(dr["menu_ID"].ToString())+"'";
  MySqlCommand cmd2 = new MySqlCommand(query2, connection2);
  MySqlDataReader dr2 = cmd2.ExecuteReader();
  double menu_cost = 0;
  while(dr2.Read())
  {
      menu_cost += Convert.ToDouble(dr2["stock_pricePerPiece"].ToString());
  }

info[1] = (dr["order_ID"].ToString());
info[2] = (dr["order_date"].ToString());
info[3] = (dr["menu_name"].ToString());
info[4] = (menu_cost.ToString("#0.00"));
info[5] = (dr["menu_price"].ToString());
info[6] = (Convert.ToDouble(info[5]) - menu_cost).ToString("#0.00");
this.ReportList.Items.Add(new ListViewItem(new string[] { info[1] , info[2] , info[3] , info[4] , info[5] , info[6] }));
}
4

4 回答 4

1

是的,您可以在一个查询中执行此操作。你可以JOIN像这样:

SELECT 
  r.* 
FROM  orderlist ol 
INNER JOIN orderdb    o ON ol.order_ID = o.order_ID
INNER JOIN menudb     m ON ol.menu_ID  = m.menu_ID
INNER JOIN recipelist r ON r.menu_ID   = m.menu_ID
INNER JOIN stocksdb   s ON r.stock_ID  = s.stock_ID 
WHERE o.order_date >= ...
  AND o.order_date <= ...;

更新:要选择 total menu_cost,这是菜单上使用的所有库存项目的总成本:

SELECT 
  s.menu_cost,
  ...
FROM  orderlist ol 
INNER JOIN orderdb    o ON ol.order_ID = o.order_ID
INNER JOIN menudb     m ON ol.menu_ID  = m.menu_ID
INNER JOIN recipelist r ON r.menu_ID   = m.menu_ID
INNER JOIN
( 
   SELECT stock_ID, SUM(stock_pricePerPiece) menu_cost
   FROM stocksdb 
   GROUP BY stock_ID
)  s ON r.stock_ID  = s.stock_ID 
WHERE o.order_date >= ...
  AND o.order_date <= ...;
于 2012-12-28T05:59:06.770 回答
1
string query = "SELECT ol.*, " + 
           "(SELECT SUM(stock_pricePerPiece) FROM recipelist r INNER JOIN stocksdb s ON r.stock_ID = s.stock_ID WHERE r.menu_ID = ol.menu_ID ) " +
           "AS stock_pricePerPiece FROM orderlist ol "+
           "INNER JOIN orderdb o "+
           "ON ol.order_ID = o.order_ID "+
           "INNER JOIN menudb m "+
           "ON ol.menu_ID = m.menu_ID "+ 
           "WHERE o.order_date >= '"+fromdate.Date()+"' AND "+
                 "o.order_date <= '"+todate.Date()+"'";

并从第一个数据读取器获取 menu_cost,您不需要第二个数据读取器

menu_cost =  Convert.ToDouble(dr["stock_pricePerPiece"].ToString());
于 2012-12-28T06:02:26.380 回答
1

你这样做的方式可能会给你带来真正的性能问题,动态创建 SQL 并执行它们并不是真正有效的,因为它们在时间上很昂贵,你应该把这项工作留给 DBMS,使用连接视图来构建你想要的数据集和让你的数据库管理这个,然后执行一条语句,你会感觉到大量数据的不同。

仅针对您感兴趣的情况,您可以生成查询的执行计划以查看其性能如何,不了解 mysql 但其他 DBMS(如 SQLServer)甚至为您提供优化步骤,以更好的性能实现相同的结果。

于 2012-12-28T06:03:45.110 回答
0

这是我根据累积的答案进行的 SQL 查询。我只是觉得我应该和大家分享一下。非常感谢您的回答!

SELECT 
DISTINCT o.order_ID, o.order_date, o.order_status, m.*, mc.menu_cost
FROM  orderlist ol 
INNER JOIN orderdb    o ON ol.order_ID = o.order_ID
INNER JOIN menudb     m ON ol.menu_ID  = m.menu_ID
INNER JOIN
(SELECT SUM(s.stock_pricePerPiece * r.recipe_quantity) AS menu_cost, m.menu_ID
    FROM recipelist r
        INNER JOIN stocksdb s ON r.stock_ID = s.stock_ID
        INNER JOIN menudb m ON r.menu_ID= m.menu_ID
    GROUP BY r.menu_ID
) 
mc ON m.menu_ID = mc.menu_ID
WHERE o.order_date >= '2012-12-22 00:00:00'
  AND o.order_date <= '2012-12-22 23:59:59';
于 2012-12-28T15:51:48.110 回答