MySQL字符串选择命令

标签 mysql sql string function

如何选择特定于字符串值的行

SELECT * FROM my_table WHERE name=
   -- then some function to test what string it starts with

它有点难以解释,所以我将用 JavaScript 来解释一个例子

if(mystring.indexOf('targetstring') != -1){
// if that variable contains this string

}

if(mystring.indexOf('targetstring') == 0){
// if that variable starts with this string

}

总而言之,我想选择行,当它们的名称(字符串列)以特定字符串开头或包含特定字符串时。

最佳答案

您可以使用 LIKE和通配符 %:

SELECT * FROM my_table WHERE name LIKE 'john%'; /* name starts with john*/ 

SELECT * FROM my_table WHERE name LIKE '%john%'; /* name contains john*/ 

关于MySQL字符串选择命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373077/

相关文章:

php - 防止数据库中的重复,而不是 ID 列

java - 如何分离字符串

mysql - 如何为特定月份选择最近一个月的最后一天?

c# - 服务器的全局文化在不同服务器上不同时的 DateTime 问题

Java 结果集 : Does the DB only update if updateRow is called?

arrays - 按空格将字符串拆分为数组

c# - 替换字符串并忽略下划线

php - 使用 php 更新 Mysql

sql - MySQL Workbench 中的列标志是什么意思?

php - 遍历记录,更新一条记录,然后退出