在我的数据库表中,我有三个字段。假设字段名称是 name、age 和 favorite_numbers。
现在,我的数据库内容如下:
- 安娜 15 岁,她最喜欢的数字是“14、21、16”
- Jessica 20 岁,她最喜欢的数字是“21、30、20”
- Sara 30 岁,她最喜欢的数字是“50、45、21”
- Jessa 12 岁,她最喜欢的数字是“10、12、20”
- Farrah 19 岁,她最喜欢的数字是“6、12、20”
如何制定我的查询以查看最喜欢的数字为 21 的人的姓名?
在上面的内容中,我只想显示 Anna、Jessica 和 Sara 的名字,因为他们三个的最喜欢的数字是 21。
最佳答案
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL.
SQL Fiddle DEMO
请注意使用
WHERE nums LIKE '%21%'
将不起作用,因为那样还包括 211 和 121
关于php - 查询 : Find string in database field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602637/