Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

mysql - Searching a string in a column

can anyone help me. i have a DB in mysql, and need to search for a string in a particular column.

the field is var char, and contains various serial number, divided by the character "/".

example

613003593/8876572/TJMC49

the problem is searching in the string. If i use like, it will work most of the times, but not always, because if i do a like '%13003593%' it will return one row, when that is not true, the saved value is 613003593. how can i search, the string.

on the example there are 3 strings divided, and i need to search all of them.

apologies for my english


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For the first part of the serial number,

SELECT * FROM  table_name
    WHERE SUBSTRING_INDEX(serial_number, '/', 1) = '613003593';

For the 2nd part,

SELECT * FROM table_name
    WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(serial_number, '/', 2), '/', -1)='8876572';

For the last part,

SELECT * FROM table_name
    WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(serial_number, '/', 3), '/', -1)='TJMC49';

Check How to split and search in comma-separated string in MySQL


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...