Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从两个表中进行选择,并对其中一个列和group by itemkey中的值求和_C#_Mysql - Fatal编程技术网

C# 从两个表中进行选择,并对其中一个列和group by itemkey中的值求和

C# 从两个表中进行选择,并对其中一个列和group by itemkey中的值求和,c#,mysql,C#,Mysql,我只是在寻求帮助,因为我一直在寻找如何解决c mysql代码。我有两个表,一个用于股票上市,另一个用于监控销售 SELECT inventory.Item, inventory.buyingPrice, inventory.sellingPrice, sales.ItemKey, SUM(sales.QuantitySales) As Qsales FROM sales INNER JOIN inventory ON sal

我只是在寻求帮助,因为我一直在寻找如何解决c mysql代码。我有两个表,一个用于股票上市,另一个用于监控销售

SELECT 
    inventory.Item, 
    inventory.buyingPrice, 
    inventory.sellingPrice, 
    sales.ItemKey, 
    SUM(sales.QuantitySales) As Qsales 
FROM 
    sales 
    INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey 
WHERE 
    sales.dateSales BETWEEN '2019/11/12' AND '2019/11/18'
GROUP BY 
    sales.ItemKey, 
    inventory.Item, 
    inventory.buyingPrice, 
    inventory.sellingPrice
库存表

销售表

数据网格视图上显示了什么

    | ItemKey    | Qtn | Product Name | Buying Price| Selling Price
    ----------------------------------------------------
    | 1572661229 | 25   |  blue band   | 20          | 30
    | 1572661899 | 35   |  salt        | 10          | 20
    | 1572661234 | 20   |  sugar       | 80          | 100
    | 1572664312 | 60   |  maize flour | 100         | 150
------------------------------------------------------------
    | TOTAL      | 140  |
我希望在datagridview上显示的内容

    | ItemKey    | Qtn | Product Name | Buying Price| Selling Price
    ----------------------------------------------------
    | 1572661229 | 5   |  blue band   | 20          | 30
    | 1572661899 | 7   |  salt        | 10          | 20
    | 1572661234 | 4   |  sugar       | 80          | 100
    | 1572664312 | 12  |  maize flour | 100         | 150
------------------------------------------------------------
    | TOTAL      | 28  |
该代码在单个日期运行良好,但当我检索整个销售期间的数据时,检索到的数据已乘以某个系数。请帮我解决上面的代码

完整代码

public void getSalesData(String date, DataGridView listV, String Seller, String[] SalesPeriod)
    {
        SqlCommand cmd;
        SqlDataAdapter adapter;
        DataTable dt = new DataTable();
        listV.Rows.Clear();
        int bpTotal = 0;
        int spTotal = 0;
        int profit = 0;
        int i = 0;
        int qtnFinalTotal = 0;
        int bpFinalTotal = 0;
        int spFinalTotal = 0;
        int finalProfit = 0;
        String sql;

        if(string.Equals(Seller, "10")){
            sql = "SELECT inventory.Item, inventory.buyingPrice, inventory.sellingPrice, sales.ItemKey, SUM(sales.QuantitySales) As Qsales FROM sales INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey WHERE sales.dateSales BETWEEN '" + SalesPeriod[0] + "' AND '" + SalesPeriod[1] + "' GROUP BY sales.ItemKey, inventory.Item, inventory.buyingPrice, inventory.sellingPrice";
            //sql = "SELECT MAX(inventory.Item) As Product, inventory.buyingPrice, inventory.sellingPrice, sales.ItemKey, SUM(sales.QuantitySales) As Qsales FROM sales INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey WHERE sales.dateSales BETWEEN '" + SalesPeriod[0] + "' AND '" + SalesPeriod[1] + "' GROUP BY sales.ItemKey, inventory.buyingPrice, inventory.sellingPrice";
        }
        else
        {
            sql = "SELECT inventory.Item, inventory.buyingPrice, inventory.sellingPrice, sales.ItemKey, SUM(sales.QuantitySales) As Qsales FROM sales INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey WHERE sales.dateSales BETWEEN '" + SalesPeriod[0] + "' AND '" + SalesPeriod[1] + "' AND inventory.date BETWEEN '" + SalesPeriod[0] + "' AND '" + SalesPeriod[1] + "' AND Sales.SellerId='" + Seller + "' GROUP BY sales.ItemKey, inventory.Item, inventory.buyingPrice, inventory.sellingPrice";
        }

        cmd = new SqlCommand(sql, con);

        try
        {
            con.Open();

            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);

            foreach (DataRow col in dt.Rows)
            {
                int n = listV.Rows.Add();
                i++;

                listV.Rows[n].Cells[0].Value = col["ItemKey"].ToString();
                listV.Rows[n].Cells[1].Value = col["Item"].ToString();
                listV.Rows[n].Cells[2].Value = col["Qsales"].ToString();
                listV.Rows[n].Cells[3].Value = col["buyingPrice"].ToString();

                bpTotal = Convert.ToInt32(col["buyingPrice"]) * Convert.ToInt32(col["Qsales"]);
                listV.Rows[n].Cells[4].Value = bpTotal.ToString();

                listV.Rows[n].Cells[5].Value = col["sellingPrice"].ToString();

                spTotal =Convert.ToInt32(col["sellingPrice"]) * Convert.ToInt32(col["Qsales"]);
                listV.Rows[n].Cells[6].Value = spTotal.ToString();

                profit = spTotal - bpTotal;

                listV.Rows[n].Cells[7].Value = profit.ToString();

                qtnFinalTotal = qtnFinalTotal + Convert.ToInt32(col["Qsales"]);
                bpFinalTotal = bpFinalTotal + bpTotal;
                spFinalTotal = spFinalTotal + spTotal;
                finalProfit = spFinalTotal - bpFinalTotal;
            }

            listV.Rows[i].Cells[1].Value = "TOTAL";
            listV.Rows[i].Cells[2].Value = qtnFinalTotal.ToString();
            listV.Rows[i].Cells[4].Value = bpFinalTotal.ToString();
            listV.Rows[i].Cells[6].Value = spFinalTotal.ToString();
            listV.Rows[i].Cells[7].Value = finalProfit.ToString();

            dt.Rows.Clear();

            //Add Styling
            listV.Rows[i].DefaultCellStyle.BackColor = Color.Beige;
            listV.Rows[i].DefaultCellStyle.Font = new Font("Tahoma", 9, FontStyle.Bold);
            dt.Rows.Clear();

            con.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            con.Close();
        }
    }

谢谢你们试着回答我的问题。最后我得到了答案。我在stock表中添加了一个新的日期列,然后添加了下面的行

AND sales.dateSales = inventory.date
从此改变

SELECT 
    inventory.Item, 
    inventory.buyingPrice, 
    inventory.sellingPrice, 
    sales.ItemKey, 
    SUM(sales.QuantitySales) As Qsales 
FROM 
    sales 
    INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey 
WHERE 
    sales.dateSales BETWEEN '2019/11/12' AND '2019/11/18'
GROUP BY 
    sales.ItemKey, 
    inventory.Item, 
    inventory.buyingPrice, 
    inventory.sellingPrice
对此

    SELECT 
        inventory.Item, 
        inventory.buyingPrice, 
        inventory.sellingPrice, 
        sales.ItemKey, 
        SUM(sales.QuantitySales) As Qsales 
    FROM 
        sales 
        INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey 
    WHERE 
        sales.dateSales BETWEEN '2019/11/12' AND '2019/11/18' AND sales.dateSales = inventory.date
    GROUP BY 
        sales.ItemKey, 
        inventory.Item, 
        inventory.buyingPrice,
        inventory.sellingPrice

它工作得很好

工作得很好,你能给我们更多的细节吗?否则它看起来很好谢谢你的回复,我已经看过你的答案了,非常棒,但是你离开了dateSales seletionI刚刚被困的地方,因为它仍然返回相同的数据和乘数。。检查editHi D Shih,你的代码运行良好,但是当我使用c时,他们的行为有些不端
SELECT 
    inventory.Item, 
    inventory.buyingPrice, 
    inventory.sellingPrice, 
    sales.ItemKey, 
    SUM(sales.QuantitySales) As Qsales 
FROM 
    sales 
    INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey 
WHERE 
    sales.dateSales BETWEEN '2019/11/12' AND '2019/11/18'
GROUP BY 
    sales.ItemKey, 
    inventory.Item, 
    inventory.buyingPrice, 
    inventory.sellingPrice
    SELECT 
        inventory.Item, 
        inventory.buyingPrice, 
        inventory.sellingPrice, 
        sales.ItemKey, 
        SUM(sales.QuantitySales) As Qsales 
    FROM 
        sales 
        INNER JOIN inventory ON sales.ItemKey=inventory.ItemKey 
    WHERE 
        sales.dateSales BETWEEN '2019/11/12' AND '2019/11/18' AND sales.dateSales = inventory.date
    GROUP BY 
        sales.ItemKey, 
        inventory.Item, 
        inventory.buyingPrice,
        inventory.sellingPrice