SQL多條件查詢拼接in中條件方法

來源:互聯網
上載者:User

做了一個功能,就是前台可以選擇同一欄位下面的多個不同的條件,這就涉及到了SQL語句拼接的問題。

下面是一個用正則來處理查詢條件的方法,是我目前所見到最簡單的方法,現將它分享給大家。

using System.Text;

using System.Text.RegularExpressions;

string  strwhere="1,11,111,1111,";

strwhere = string.Format("select

 * from test where strwhere in ({0})",Regex.Replace(strwhere.TrimEnd(','), @"[^,]+", "'$0'"));

註:這裡strwhere.TrimEnd(',')用來將1111後面的逗號去掉。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.