如何选择特定于字符串值的行
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/