简体   繁体   English

如何选择类似于字符串的前n个字符的标题?

[英]How to select titles that are like the first n characters of a string?

Here's an example 这是一个例子

There are titles of some animes: 有一些动漫的标题:

|_title_|
|My hero Academia|
|My hero Academia Season 2|
|My hero Academia Season 3|
|My hero Academia Season 4|

And I'd like to select those titles, that contain the first 10 character of the first record, so "My Hero Academia" in this example 我想选择包含第一条记录的前10个字符的标题,因此在此示例中为“我的英雄学院”

It's important that it must be the first N characters, like 10,15,20. 重要的是它必须是前N个字符,例如10、15、20。 So SELECT title FROM tablename WHERE title LIKE "%My hero Academia%" is not the solution I'm looking for. 因此,在SELECT title FROM tablename WHERE title LIKE "%My hero Academia%"这样的SELECT title FROM tablename WHERE title LIKE "%My hero Academia%"不是我要的解决方案。

Also: this is just an example,there are many other titles too, and I don't always know what the first 10 characters are. 另外:这只是一个例子,还有许多其他标题,我并不总是知道前10个字符是什么。 So I also can't simply put the first 10 characters of "My hero academia" in the WHERE title LIKE clause. 因此,我也不能简单地在WHERE title LIKE子句中输入“我的英雄学院”的前10个字符。

I mean something like this: 我的意思是这样的:

SELECT title FROM table_name WHERE title LIKE the first 10 characters of a given string

Is there a way to do that with SQL? 有没有办法用SQL做到这一点? Thank you for any help in advance! 预先感谢您的帮助!

for filter a string for a partial fixed value you can use like operator like 'yourstring%' eg: 对于部分固定值的字符串过滤,可以使用like 'yourstring%'这样的运算符,例如:

    SELECT title 
    FROM your_table  
    WHERE title LIKE 'thefirst10%'  

or use a string fuction like substr or left 或使用类似substr或left的字符串功能

    SELECT title 
    FROM your_table  
    WHERE left(title,10) = 'thefirst10'  

    SELECT title 
    FROM your_table  
    WHERE substr(title,1,10) = 'thefirst10'  

像这样输入10个字符:

WHERE title LIKE 'aaaaaaaaaa%'
WHERE SUBSTRING(title, 1, n) = 'aaaaaaaaa'

尝试这个:

SELECT title FROM table_name WHERE title like "data structure%"
SELECT title FROM TABLE_NAME WHERE title LIKE '%'  + substring('AAA' , 0 ,2)

substring函数的第一个参数代表您的字符串,第二个开始索引和第三个结束索引。您可以根据需要更改结束索引。

使用通配符(%):

SELECT title FROM table_name WHERE title LIKE "1234567890%"

this could help you it qorks for my postgray11. 这可以帮助您为我的postgray11赞一下。 ithink it will hwlp for your mysql ithink它将为您的mysql hwlp

select * from t1 where title LIKE CONCAT((select * from t1 LIMIT 1),'%');

demo here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM