用于连接表的SQL语句 - 没有任何工作

时间:2014-04-30 09:52:51

标签: sql httpcontext

此语句正在提取excel的列表,其工作正常

string sql = "select wo.email, wo.productid, wo.variantid ";
sql += "from woeosemails wo ";
sql += "order by email, productid ";

string attachment = "attachment; filename=EmailList.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");

HttpContext.Current.Response.Write("email,productid,variantid");
HttpContext.Current.Response.Write(Environment.NewLine);

using (SqlConnection conn = new SqlConnection(DB.GetDBConn()))
{
conn.Open();
using (IDataReader NotifyReader = DB.GetRS(sql, conn))
{
while (NotifyReader.Read())
{
string email = DB.RSField(NotifyReader, "email");
int productid = DB.RSFieldInt(NotifyReader, "productid");
int variantid = DB.RSFieldInt(NotifyReader, "variantid");

email = email.Replace("\"","\"\"");
HttpContext.Current.Response.Write("\"" + email + "\"," + productid.ToString() + "," + variantid.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);

}
}
conn.Close();
}

HttpContext.Current.Response.End();

我尝试添加各种各样的内容,我认为这样可行

我想从另一个sql表中添加两列,这些是Name和SKU。关于我如何修改此代码的第一部分的任何想法,我已尝试加入表但似乎没有任何工作。我最接近的是将第一部分修改为此

string sql = "select wo.email, wo.productid, wo.variantid,  p.Name pname,  p.Name psku, ";
        sql += "from woeosemails wo ";
                    sql += "join Product p with (nolock) on p.ProductID = wo.productid ";
        sql += "order by email, productid ";

任何帮助都会很棒

2 个答案:

答案 0 :(得分:1)

首先,您需要按属性为您的订单添加别名,并在结尾的第一行删除逗号。请尝试以下方法:

string sql = "select wo.email, wo.productid, wo.variantid,  p.Name pname,  p.Name psku ";
sql += "from woeosemails wo ";
sql += "join Product p with (nolock) on p.ProductID = wo.productid ";
sql += "order by wo.email, wo.productid ";

答案 1 :(得分:0)

订单可能会弄乱这个,你有两个ProductID,所以你需要指定哪一个,即:wo.productid。同样查看电子邮件。