Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为下面给出的查询编写等效的Sqlite语句?_Sql_Sqlite - Fatal编程技术网

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

如何为下面给出的查询编写等效的Sqlite语句?,sql,sqlite,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

这是我的工作非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语句

此查询中的问题是:

如果文本框中未提供任何内容,则它将从db tablt中搜索NULL或“”(空字符串)。如果文本框中未输入任何内容,则属性将从where子句中跳过,并且只考虑文本框中提供的值来签入where子句,则我希望与上述代码一样。在这种情况下,sql case和if else条件是否有帮助?有人能告诉我怎样才能做到这一点吗?谢谢

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

使用条件

(@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+'%'))
有关提供“”而不是空参数的信息,请参见下面的注释。否则情况就太糟糕了,例如:

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

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

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

最后,我提出了符合我要求的查询

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)  

我也尝试过这个方法,得到了相同的结果:选择客户Id、客户标题、卡号、钥匙人、当前地址、电话号码、手机号码、新客户的区域位置,其中(@Clients\u Title=''或客户标题,如“%”++@Clients\u Title++')和(@Address\u Current=''或当前地址,如“%”++@Address\u Current++'))和(@Phone_Number=''或Phone_Number,如“%”++@Phone_Number++“%”)和(@Mobile_Number=''或Mobile_Number,如“%”++@Mobile_Number++“%”)和(@AreaLocation=''或AreaLocation,如“%”++@AreaLocation++“%”)您的示例是否不起作用???我的非sql查询起作用..但我想编写等效的sql查询..我的sql查询不起作用请编写能够满足该场景的完整查询。我认为您编写的条件不会满足,因为如果文本框中输入的值为“”或null,它不会跳过where子句中的任何条件。您的查询将搜索空表单db表..正如我在问题中提到的;如果值为NULL,where子句中的条件应跳过。数据如何可以为“”或NULL?如果框留空,则为变量指定了什么值?您的输入应该将2中的一个写入变量,而不是两个都写入。假设一个空文本框将变量设置为“”,则上述方法效果良好。或者您是在指示单个空格而不是空白值
'
'
您可以添加NULL检查,但是文本框中应该有零长度字符串,而不是没有字符串;或者,如果参数为NULL,则向SQL提供参数的代码可以提供“”。否则,查询会变得非常糟糕。上面的示例。确定。保留null或“”(空字符串)一个方面。我已经尝试了类似的结果……我必须搜索上面的查询。在WHERE子句中我需要的是如果没有提供属性值,则查询子句将它视为不在CULSE的部分。如果没有提供属性值,CULSU.Quess只考虑WHERE子句不存在,因为WHERE子句。没有提供属性值…如何为这种情况编写查询?
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)