如何为下面给出的查询编写等效的Sqlite语句?

时间:2013-11-18 17:21:35

标签: sql sqlite

这是我工作的非SQL查询:

Query = "select Cust_Id,Card_Number,Clients_Title,Address_Current,Phone_Number,Mobile_Number from Customer_New Where 1=1";
try
{
    if (txt_title.Text != "")
        Query += " and Clients_Title Like '%" + txt_title.Text + "%'";
    if (txt_address.Text != "")
        Query += " and Address_Current Like '%" + txt_address.Text + "%'";
    if (txt_phone.Text != "")
        Query += " and Phone_Number Like '%" + txt_phone.Text + "%'";
    if (txt_mobile.Text != "")
        Query += " and Mobile_Number Like '%" + txt_mobile.Text + "%'";
    if (cbo_location.Text != "")
        Query += " and AreaLocation Like '%" + cbo_location.Text + "%'";
}

catch { }

在此代码中,“where”子句的工作是:

  • 如果所有文本框都为空,则选择所有记录表示跳过哪里 条款

  • 如果在任何1个文本框中输入了一些文本,则在“where”中输入 子句是在该特定文本框上进行的

  • 如果在任何多个文本框中输入了某些文本,则输入    “where”条款是通过履行“AND”根据它们制作的    它们之间的条件。它意味着所有值都输入到文本框中    必须与相应的行属性匹配

  

这里我试图编写它的等效SQL语句。

SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
WHERE (@Clients_Title IS NULL   OR   Clients_Title    = @Clients_Title )
AND (@Address_Current IS NULL   OR   Address_Current  = @Address_Current )
AND (@Phone_Number    IS NULL   OR   Phone_Number     = @Phone_Number)
AND (@Mobile_Number   IS NULL   OR   Mobile_Number    = @Mobile_Number )
AND (@AreaLocation    IS NULL   OR   AreaLocation     = @AreaLocation)

此查询中的问题是:

如果文本框中没有提供任何内容,它会从db tablt中搜索NULL或''(空字符串)。如上所述,如果在文本框中没有输入任何内容,则从where子句中跳过属性并且只在文本框中提供值考虑检查where子句。可以在这种情况下使用sql case和if else条件帮助吗?谁能告诉我如何才能做到这一点?感谢

注意:在sqlite存储过程中不要退出。所以指导我根据场景编写正确的sql查询。

3 个答案:

答案 0 :(得分:0)

使用条件

(@Clients_Title = ''
 OR ((@Clients_Title != '') AND (Clients_Title = @Clients_Title)

等等。

这是完整版

SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current,
       Phone_Number, Mobile_Number, AreaLocation FROM Customer_New 
WHERE (@Clients_Title = '' 
       OR (@Clients_Title != '' AND Clients_Title LIKE '%'+@Clients_Title+'%'))
  AND (@Address_Current = '' 
       OR (@Address_Current != '' AND Address_Current LIKE '%'+@Address_Current+'%'))
  AND (@Phone_Number = '' 
       OR (@Phone_Number != '' AND Phone_Number LIKE '%'+@Phone_Number+'%'))
  AND (@Mobile_Number = '' 
       OR (@Mobile_Number != '' AND Mobile_Number LIKE '%'+@Mobile_Number+'%'))
  AND (@AreaLocation = '' 
       OR (@AreaLocation != '' AND AreaLocation LIKE '%'+@AreaLocation+'%'))

请参阅下面的注释,提供''而不是NULL参数。否则条件会变得太难看,例如:

  AND (@AreaLocation = '' OR @AreaLocation IS NULL
       OR ((@AreaLocation != '' AND @AreaLocation IS NOT NULL)
            AND AreaLocation LIKE '%'+@AreaLocation+'%'))

答案 1 :(得分:0)

您可能需要测试参数是否为空字符串,以便您可以执行

AND (@param IS NULL OR @param = '' OR @param = param)

答案 2 :(得分:0)

最后我做了满足我要求的查询

SELECT        Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM            Customer_New
    where        (CASE
        WHEN @Clients_Title != ''   THEN Clients_Title=@Clients_Title
      ELSE
         NULL IS NULL
      END)
      AND(CASE
        WHEN @Address_Current != '' THEN Address_Current =@Address_Current
      ELSE
         NULL IS NULL
      END)
              AND(CASE
        WHEN @Phone_Number != '' THEN Phone_Number=@Phone_Number
      ELSE
         NULL IS NULL
      END)
      AND(CASE
        WHEN @Mobile_Number != '' THEN Mobile_Number=@Mobile_Number
      ELSE
         NULL IS NULL
      END)
      AND(CASE
        WHEN @AreaLocation != '' THEN AreaLocation =@AreaLocation
      ELSE
         NULL IS NULL
      END)