且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

where_in和find_in_set之间的区别

更新时间:2023-12-04 11:31:10

WHERE IN要求在查询中按字面值指定一组值,而不是包含逗号分隔字符串的单个值.如果您写:

WHERE IN requires the set of values to be specified literally in the query, not as a single value containing a comma-delimited string. If you write:

WHERE 6 IN (a.allowed_activity)

它将a.allowed_activity视为单个值,并将其与6进行比较,而不是将其作为多个要搜索的值的集合.

it will treat a.allowed_activity as just a single value, and compare it with 6, not as a set of multiple values to search.

FIND_IN_SET在逗号分隔的字符串中搜索值.

FIND_IN_SET searches a comma-delimited string for the value.

另一种查看方式是IN是结合OR的一堆=测试的快捷方式:

Another way to view it is that IN is a shortcut for a bunch of = tests combined with OR:

WHERE x IN (a, b, c, d)

WHERE x = a OR x = b OR x = c OR x = d

以这种方式重写它时,您可以清楚地知道为什么它不适用于包含逗号分隔的字符串的列.它只是翻译

When you rewrite it like this, you can see clearly why it won't work with a column containing a comma-delimited string. It simply translates

WHERE 6 IN (a.allowed_activity) 

收件人:

WHERE 6 = a.allowed_activity