mysql - select locate not working well -


not sure how explain this. have following sample table

id          participants 13          128, 125 18          122, 125 29          182, 125 34           17, 12 38           18, 15 

i want count of messages 12 on right using following query, select count(id) messages table locate (12, participants, 2) problem query returns results 12 e.g. 125, instead of having 1 count have 4 counts incorrect. suggestions on query use?

you can result of substring_index , ', ' delimiter:

select * your_table     substring_index(participants, ', ', -1) = 12; 

if can't sure blank after comma, use

select * your_table     trim(substring_index(participants, ',', -1)) = 12; 

instead.

demo

note
better not store delimited lists of values in 1 field, if can. it's asking trouble , bad performance, because mysql can't use index kind of condition. it's sound advice of marc normalize tables, rid of problem , gain performance use of index.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -