簡體   English   中英

如何選擇類似於字符串的前n個字符的標題?

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

這是一個例子

有一些動漫的標題:

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

我想選擇包含第一條記錄的前10個字符的標題,因此在此示例中為“我的英雄學院”

重要的是它必須是前N個字符,例如10、15、20。 因此,在SELECT title FROM tablename WHERE title LIKE "%My hero Academia%"這樣的SELECT title FROM tablename WHERE title LIKE "%My hero Academia%"不是我要的解決方案。

另外:這只是一個例子,還有許多其他標題,我並不總是知道前10個字符是什么。 因此,我也不能簡單地在WHERE title LIKE子句中輸入“我的英雄學院”的前10個字符。

我的意思是這樣的:

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

有沒有辦法用SQL做到這一點? 預先感謝您的幫助!

對於部分固定值的字符串過濾,可以使用like 'yourstring%'這樣的運算符,例如:

    SELECT title 
    FROM your_table  
    WHERE title LIKE 'thefirst10%'  

或使用類似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%"

這可以幫助您為我的postgray11贊一下。 ithink它將為您的mysql hwlp

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

在這里演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM