gpt4 book ai didi

c# - 是否有比我现在正在做的更有效的方法从 5 个表中获取数据?

转载 作者:行者123 更新时间:2023-11-29 05:31:49 27 4
gpt4 key购买 nike

所以我想做的是使用 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] }));
}

最佳答案

是的,您可以在一个查询中执行此操作。您可以像这样加入它们:

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 <= ...;

关于c# - 是否有比我现在正在做的更有效的方法从 5 个表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com