【问题标题】:How to write this condition in sql?如何在sql中写这个条件?
【发布时间】:2014-02-01 10:30:37
【问题描述】:

我有一个问题。我想用sql写这个条件:

AnyString.StartsWith(String1 + "_" + String2) or AnyString.StartsWith(String2 + "_" + String1)

比如我想做一个函数:

string commandString = "UPDATE [AnyTable] Set [AnyColumn]='Something' WHERE and here the condition above";

【问题讨论】:

    标签: c# sql conditional-statements


    【解决方案1】:

    选择:

    Select *
    From Table1
    Where Field1 Like string1 + '[_]' + string2 + '%'
    Or Field1 Like string2 + '[_]' + string1 + '%'
    

    更新:

    Update Table1
    Set Field2 = 'YourValue'
    Where Field1 Like string1 + '[_]' + string2 + '%'
    Or Field1 Like string2 + '[_]' + string1 + '%'
    

    【讨论】:

    • 这将匹配由下划线以外的字符分隔的字符串,因为LIKE 中的_ 匹配任何字符。您需要对其进行转义才能使查询正常运行。
    【解决方案2】:
    Update Table_A
    set Column_B = 'WhatEver'
    where column_C like String1 + '[_]' + String2 +'%' 
    or column_C like String2 + '[_]' + String1 +'%'
    

    【讨论】:

    • 使用参数防止sqli
    • 我没有用引号正确处理字符串(我只是懒惰地复制了上面的字符串)
    • 我现在感觉好多了,我清理了我的弦
    • 这将匹配由下划线以外的字符分隔的字符串,因为LIKE 中的_ 匹配任何字符。您需要对其进行转义才能使查询正常运行。
    • 你是完全正确的!我很少使用 % 之外的任何东西,我什至没有考虑过!好收获!
    【解决方案3】:
    string commandString = "UPDATE [AnyTable] 
    Set [AnyColumn]='Something' 
    WHERE AnyString like String1 + "_" + String2 + "%" or AnyString like String2 + "_" + String1 + "%";
    

    【讨论】:

      【解决方案4】:

      试试这个:

      "UPDATE [AnyTable] Set [AnyColumn]='Something' WHERE first_col LIKE 
      sometext_othertext% OR second_col LIKE sometext_othertext%"
      

      使用 % 您可以选择以下一个单词开头的文本句子。

      http://technet.microsoft.com/en-us/library/ms179859.aspx (SQL LIKE)

      【讨论】:

        【解决方案5】:

        select * from Foo where Bar LIKE 'string%'

        (其中 % 是通配符,表示匹配任何内容)

        【讨论】:

        • % on end 对应的 StartsWith
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 1970-01-01
        • 2012-11-01
        • 2021-04-18
        • 2016-03-24
        • 1970-01-01
        相关资源
        最近更新 更多