尝试将 where 子句附加到 sql 语句

发布于 2024-11-28 06:03:01 字数 3237 浏览 2 评论 0原文

您好,我正在尝试将 where 子句附加到这样的 sql 语句中,

    public static DataTable paymentType(string paymenttype, string ddproviders, string overdue)
    { 

                string paymenttypestr = "";
    string ddproviderstr = "";


    if (paymenttype != "")
    {
        paymenttypestr = string.Format("AND membertomships.memberToMship_PayMethod = '{0}'", paymenttype);        

    }
    if (ddproviders != "")
    {
        ddproviderstr = string.Format("AND ddproviders.ddProvider_Name = '{0}'", ddproviders);

    }
    if (overdue == "OverDue-Now")
    { 
       string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";

    }

    string  sql = string.Format(
                @"SELECT    members.member_Id,
                   members.member_Lastname As Last_name,
                   members.member_Firstname AS First_name,
                   members.member_PostCode  As Post_Code, 
                   ddaccounts.ddAccount_DdReference As dd_reference,
                   ddproviders.ddProvider_Name As dd_providername,
                   memberToMship_ChargePerPeriod As monthly_amount,
                   mshiptypes.mshipType_Name As Membership_type,
                   mshipstatustypes.mshipStatusType_Name As Status,
                   membertomships.memberToMship_EndDate As Expiry_Date,
                   membertomships.memberToMship_PayMethod As payment_method
                   FROM members 
                   LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
                   LEFT JOIN memberpaysched ON memberpaysched.memberPaySched_memberId = members.member_Id
                   LEFT OUTER JOIN ddaccounts ON ddaccounts.member_Id = members.member_Id
                   LEFT OUTER JOIN  ddproviders ON  ddaccounts.ddProvider_Id=ddproviders.ddProvider_Id 
                   LEFT JOIN   mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
                   LEFT JOIN   mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
                   LEFT JOIN   mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
                   WHERE       members.member_Active LIKE 'y%'
                   AND               mshipoptions.mshipOption_Period = 'month' 
                   AND               (mshipstatustypes.mshipStatusType_Id <> 5)
                   {0}
                   {1}     
                   ORDER BY members.member_Lastname",
                   paymenttypestr, ddproviderstr);
      return getdata(sql,mf);
    }

但它在这一行给出了错误,如“;”需要这个符号;

    string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid "; 

我想添加这个 where 子句 if overdue == "overdue-now"

我怎样才能根据条件添加这个 where 子句,

任何人都会对此提供帮助....

Modified Query :   string sql += @"memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid";

仍然显示此错误 “无效的表达术语+=”

HiI am trying to attach the where clause to sql statement like this

    public static DataTable paymentType(string paymenttype, string ddproviders, string overdue)
    { 

                string paymenttypestr = "";
    string ddproviderstr = "";


    if (paymenttype != "")
    {
        paymenttypestr = string.Format("AND membertomships.memberToMship_PayMethod = '{0}'", paymenttype);        

    }
    if (ddproviders != "")
    {
        ddproviderstr = string.Format("AND ddproviders.ddProvider_Name = '{0}'", ddproviders);

    }
    if (overdue == "OverDue-Now")
    { 
       string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";

    }

    string  sql = string.Format(
                @"SELECT    members.member_Id,
                   members.member_Lastname As Last_name,
                   members.member_Firstname AS First_name,
                   members.member_PostCode  As Post_Code, 
                   ddaccounts.ddAccount_DdReference As dd_reference,
                   ddproviders.ddProvider_Name As dd_providername,
                   memberToMship_ChargePerPeriod As monthly_amount,
                   mshiptypes.mshipType_Name As Membership_type,
                   mshipstatustypes.mshipStatusType_Name As Status,
                   membertomships.memberToMship_EndDate As Expiry_Date,
                   membertomships.memberToMship_PayMethod As payment_method
                   FROM members 
                   LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
                   LEFT JOIN memberpaysched ON memberpaysched.memberPaySched_memberId = members.member_Id
                   LEFT OUTER JOIN ddaccounts ON ddaccounts.member_Id = members.member_Id
                   LEFT OUTER JOIN  ddproviders ON  ddaccounts.ddProvider_Id=ddproviders.ddProvider_Id 
                   LEFT JOIN   mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
                   LEFT JOIN   mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
                   LEFT JOIN   mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
                   WHERE       members.member_Active LIKE 'y%'
                   AND               mshipoptions.mshipOption_Period = 'month' 
                   AND               (mshipstatustypes.mshipStatusType_Id <> 5)
                   {0}
                   {1}     
                   ORDER BY members.member_Lastname",
                   paymenttypestr, ddproviderstr);
      return getdata(sql,mf);
    }

but it was giving error at this line like ";" this symbol is needed;

    string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid "; 

I want to add this where clause if overdue == "overdue-now"

how can i add this where clause according to condition

would any one help on this ....

Modified Query :   string sql += @"memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid";

still it was showing this error
"Invalid EXPRESSION TERM +="

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-12-05 06:03:01

乍一看你有 2 个 where 子句。更改要添加到 AND 的值,看看会发生什么。

On 1st glance you have 2 where clauses. Change the one you're adding to an AND and see what happens.

生活了然无味 2024-12-05 06:03:01

因此,如果 overdue == "OverDue-Now" 您正在声明一串 sql 并 += ing 它?但随后您会覆盖您认为刚刚创建的字符串。

我认为这就是您想要实现的目标:

if (overdue == "OverDue-Now")     
{
   string isoverduestr = @"AND memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";      
}
else
{
   string isoverduestr = @"";
}

string  sql = string.Format(
            @"SELECT    members.member_Id,
               members.member_Lastname As Last_name,
               members.member_Firstname AS First_name,
               members.member_PostCode  As Post_Code, 
               ddaccounts.ddAccount_DdReference As dd_reference,
               ddproviders.ddProvider_Name As dd_providername,
               memberToMship_ChargePerPeriod As monthly_amount,
               mshiptypes.mshipType_Name As Membership_type,
               mshipstatustypes.mshipStatusType_Name As Status,
               membertomships.memberToMship_EndDate As Expiry_Date,
               membertomships.memberToMship_PayMethod As payment_method
               FROM members 
               LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
               LEFT JOIN memberpaysched ON memberpaysched.memberPaySched_memberId = members.member_Id
               LEFT OUTER JOIN ddaccounts ON ddaccounts.member_Id = members.member_Id
               LEFT OUTER JOIN  ddproviders ON  ddaccounts.ddProvider_Id=ddproviders.ddProvider_Id 
               LEFT JOIN   mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
               LEFT JOIN   mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
               LEFT JOIN   mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
               WHERE       members.member_Active LIKE 'y%'
               AND               mshipoptions.mshipOption_Period = 'month' 
               AND               (mshipstatustypes.mshipStatusType_Id <> 5)
               {0}
               {1}
               {2}     
               ORDER BY members.member_Lastname",
               paymenttypestr, ddproviderstr, isoverduestr);

So if overdue == "OverDue-Now" you are declaring a string of sql and +=ing it? But then you overwrite the string you thought you just created.

I think this is what you're trying to accomplish:

if (overdue == "OverDue-Now")     
{
   string isoverduestr = @"AND memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";      
}
else
{
   string isoverduestr = @"";
}

string  sql = string.Format(
            @"SELECT    members.member_Id,
               members.member_Lastname As Last_name,
               members.member_Firstname AS First_name,
               members.member_PostCode  As Post_Code, 
               ddaccounts.ddAccount_DdReference As dd_reference,
               ddproviders.ddProvider_Name As dd_providername,
               memberToMship_ChargePerPeriod As monthly_amount,
               mshiptypes.mshipType_Name As Membership_type,
               mshipstatustypes.mshipStatusType_Name As Status,
               membertomships.memberToMship_EndDate As Expiry_Date,
               membertomships.memberToMship_PayMethod As payment_method
               FROM members 
               LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
               LEFT JOIN memberpaysched ON memberpaysched.memberPaySched_memberId = members.member_Id
               LEFT OUTER JOIN ddaccounts ON ddaccounts.member_Id = members.member_Id
               LEFT OUTER JOIN  ddproviders ON  ddaccounts.ddProvider_Id=ddproviders.ddProvider_Id 
               LEFT JOIN   mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
               LEFT JOIN   mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
               LEFT JOIN   mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
               WHERE       members.member_Active LIKE 'y%'
               AND               mshipoptions.mshipOption_Period = 'month' 
               AND               (mshipstatustypes.mshipStatusType_Id <> 5)
               {0}
               {1}
               {2}     
               ORDER BY members.member_Lastname",
               paymenttypestr, ddproviderstr, isoverduestr);
凯凯我们等你回来 2024-12-05 06:03:01

我不是 .NET 专家,但我可能会将:更改

if (overdue == "OverDue-Now")     
{
     string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";      
} 

为:(

if (overdue == "OverDue-Now")     
{
     sql += @"AND memberpaysched.memberPaySched_dateDue < NOW() AND   memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";      
} 

即删除字符串关键字,并用 AND 替换 WHERE)

并将其放在主 SQL 块之后(即声明字符串 sql 变量的位置) ..

I'm no .NET expert, but I'd probably change:

if (overdue == "OverDue-Now")     
{
     string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";      
} 

to:

if (overdue == "OverDue-Now")     
{
     sql += @"AND memberpaysched.memberPaySched_dateDue < NOW() AND   memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";      
} 

(i.e. remove the string keyword, and substitute AND for WHERE)

and put it AFTER the main SQL block (i.e. where you're declaring your string sql variable)..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文