【问题标题】:correct method to Use sub string in where clause mysql query在where子句mysql查询中使用子字符串的正确方法
【发布时间】:2018-03-06 19:24:06
【问题描述】:

我在mysql 中有一个表,列名为type。内容如下

Type
Test 12
Test abc
start 1
start abcd
end 123

现在我想选择typeTest开头的记录

预期结果:

Test 12
Test abc

但我得到了任何一个

测试 12 或清空结果

我试过如下:

select * from table where type = 'Test 12'
select * from table where type = '%Test%'
select * from table where type = '%Test'

正确的sql 语句应该是什么。

【问题讨论】:

    标签: mysql sql string select substring


    【解决方案1】:

    如果您想进行部分匹配,您需要 LIKE 运算符:

    SELECT * FROM table WHERE type LIKE 'Test%'
    

    【讨论】:

    • %Test 以 Test 结束 而不是以 Test 开始。
    • @JacquesAmar 已更正。
    【解决方案2】:

    使用 Like 关键字

    select * from table where type LIKE 'Test%'
    

    【讨论】:

      【解决方案3】:

      相等 (=) 运算符不接受通配符。您应该改用like 运算符:

      SELECT * FROM table WHERE type LIKE 'Test%'
      

      【讨论】:

        【解决方案4】:

        太近了!你想开始用字符串,然后不要在开头使用%,并使用LIKE而不是=

        select * from table where type LIKE 'Test%'
        

        【讨论】:

          猜你喜欢
          • 2015-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多