extracting the ids assigned to a list of user ids

喜夏-厌秋 提交于 2019-12-08 13:35:24

问题


Above question may be confusing so let me explain it. I have a table with structure similar as below :

form_id         bank_ids 

   1            5,6,7,8
   2            7,10
   3            4,7,9
   4            5,8,1

Now suppose I want to extract the form ids assigned to bank_id 7, how can I able to extract it?


回答1:


MySQL has a builtin function for that called FIND_IN_SET.

SELECT *
FROM tableName
WHERE FIND_IN_SET('7', bank_ids) > 0

You should properly normalize your table.

UPDATE

if you want to use LIKE, you need to concatenate , on both side. eg

WHERE CONCAT(',', bank_ids, ',') LIKE CONCAT('%,', '7', ',%')


来源:https://stackoverflow.com/questions/19462626/extracting-the-ids-assigned-to-a-list-of-user-ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!