如何求和2个不同的列并将其求和为不同的行

时间:2018-11-02 22:57:57

标签: sql sql-server

enter image description here

如何计算所有行的menu_price * menu_quantity和总计 在我的代码中,此代码仅用于一列,而menu_quantity则没有多个。当您看到表格时,就可以猜到我要计算总费用

string stmt = "SELECT SUM(menu_price) FROM cart Where email=@email";

try
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.
   ConnectionStrings["connectionString"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(stmt, con))
        {
            cmd.Parameters.AddWithValue("@email", Session["email"].ToString());
            con.Open();
            int count = (int)cmd.ExecuteScalar();
            return count;
        }
    }
}
catch (Exception e)
{
    lbltotalitemcart.Text = e.ToString();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您可以在SUM()内进行乘法:

SELECT SUM(menu_price * menu_quantity) FROM cart WHERE email=@email