0
点赞
收藏
分享

微信扫一扫

mysql之find_in_Set函数

1.语法

#查询出在list中包含column2的所有行。此处list可以是字段或常量

select column1 from table_name where FIND_IN_SET(column2,list)

2.与in的区别

  • in后面只能跟常量, find_in_set()函数可以使用常量或字段。
  • in是完全匹配,find_in_set()函数是精确匹配,字段值以英文”,”分隔。

3.举例

mysql之find_in_Set函数_mysql函数

查询ancestors字段中包含100且dept_id为100的记录

  • 用in查询

SELECT * FROM dept WHERE ancestors IN (100) OR dept_id =100

运行结果:

mysql之find_in_Set函数_find_in_set_02

用in则只查出dept_id为100的数据,前面因为没有与100完全匹配的ancestors所以没有显示。

  • 用find_in_set查询

SELECT * FROM dept WHERE FIND_IN_SET(100,ancestors) OR dept_id =100

运行结果:

mysql之find_in_Set函数_sql_03

用find_in_set查出ancestors里包含100的记录以及dept_id为100的记录。其中find_in_set会默认将ancestors中的值按‘,’分割。

举报

相关推荐

0 条评论