除了我现在正在做的事情之外,是否有更有效的方法从5个表中获取数据?

时间:2012-12-28 05:52:09

标签: c# mysql select join

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

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 个答案:

答案 0 :(得分: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 <= ...;

更新:选择总计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 <= ...;

答案 1 :(得分: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());

答案 2 :(得分:1)

你这样做的方式会导致你出现真正的性能问题,动态创建SQL并执行它们并不是很有效,因为它们在时间上很昂贵,你应该把这份工作交给DBMS,使用{{3或者joins来构建你想要的数据集并让你的数据库管理它,然后执行一个单一的语句,你会感觉到大量数据的差异。

就您感兴趣的情况而言,您可以生成查询的views以查看其性能如何,不了解mysql,但其他DBMS就像SQLServer一样为您提供优化步骤来实现相同的结果和更好的表现。

答案 3 :(得分: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';