Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

how select row in MySQL by check when column data include delimiter

I have question . If i have user table and a column name be freinds . I want use some user id inside a column friends with this value : 1|2|45|18 . This means for example user id =5 have 4 freinds with these id 1|2|45|18. I don't want use two table for this situation.

How can select each users data when friends with user id = 5 ? In php we have explode function for slice each delimeter . Can i set delimeter for this target or i should use two table user and friends in MySQL ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It's an awful case, especially if you inherit some DB with such a scheme, but to find friends you can use:

SELECT friend.*
FROM user u 
left join user friend on  concat('|', o.friends, '|') like concat('%|', friend.id, '|%')

where u.id = 5;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...