如何从具有字符串列的表中获取记录,其中字符串列的值以给定的子字符串开头?

时间:2022-09-25 22:13:41

Example - Sub string is "ab" And if the contents are

示例 - 子字符串为“ab”,如果内容为

abhi
babu
abdullah

Then after running the query I should be getting only the values

然后在运行查询后,我应该只获取值

abhi 
abdullah

Even though value 'babu' contains the sub string ab

即使值'babu'包含子字符串ab

Suppose the table name is person and the column name is name

假设表名是person,列名是name

2 个解决方案

#1


2  

You can use LIKE operator like this:

您可以像这样使用LIKE运算符:

SELECT * FROM person
WHERE name LIKE 'ab%'

If you add % ahead of ab then babu will also come in result so only add % after ab.

如果你在ab之前加上%,那么babu也会得到结果所以只能在ab之后加上%。

#2


1  

Use sql LIKE operator

使用sql LIKE运算符

SELECT name FROM person WHERE name LIKE 'ab%'

above query return all name starting with ab, if you want to get names like hiabhi use like this

上面的查询返回所有以ab开头的名称,如果你想获得像hiabhi这样的名字就像这样

SELECT name FROM person WHERE name LIKE '%ab%'

#1


2  

You can use LIKE operator like this:

您可以像这样使用LIKE运算符:

SELECT * FROM person
WHERE name LIKE 'ab%'

If you add % ahead of ab then babu will also come in result so only add % after ab.

如果你在ab之前加上%,那么babu也会得到结果所以只能在ab之后加上%。

#2


1  

Use sql LIKE operator

使用sql LIKE运算符

SELECT name FROM person WHERE name LIKE 'ab%'

above query return all name starting with ab, if you want to get names like hiabhi use like this

上面的查询返回所有以ab开头的名称,如果你想获得像hiabhi这样的名字就像这样

SELECT name FROM person WHERE name LIKE '%ab%'