簡體   English   中英

SQL-如何選擇列包含空格分隔的字符串的位置

[英]SQL - how to select where column contains string separated by space

我有SQL列“屬性”,其中值是:

11235 11236 11237 11238 11239 11240

在其他行(同一列)中是值:

233 234 235 236 237

我需要在列包含“ 235”的地方進行SELECT 我不能使用WHERE LIKE "%235%"因為該代碼選擇11235而我只需要235用空格分隔。

感謝幫助。

我強烈建議您不要存儲這樣的數據-如果將其標准化則更容易查詢。

但是,這是使用concat的一種選擇:

select *
from yourtable
where concat(' ', field, ' ') like '% 235 %';

多數民眾贊成在一個不好的數據庫模式,你應該考慮規范化。 這是使用find_in_set處理這種情況的一種方法

mysql> select find_in_set('11235',replace('11235 11236 11237 11238 11239 11240',' ',',')) val;
+-----+
| val |
+-----+
|   1 |
+-----+
1 row in set (0.00 sec)

所以你可以用

select * from table_name where 
find_in_set('235',replace(col_name,' ',',')) > 0

暫無
暫無

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

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