检查条件(以逗号分隔) [英] Check the conditions in comma separated values
问题描述
我正在使用CodeIgniter.在我的表中,该字段之一包含1,2或1或2之类的值.现在,我想检查模型中的条件.列名是上面提到的值的期间.
I'm using CodeIgniter. In my table one of the field contain value like 1,2 or 1 or 2. Now I want to check a condition in model. The column name is period that value is mentioned above.
模型
if($post_period == 1)
{
$this->db->where('period',1);
}
else if($post_period == 2)
{
$this->db->where('period',2);
}
推荐答案
使用Mysql函数FIND_IN_SET(str,strlist).
Use Mysql Function FIND_IN_SET(str,strlist) .
如果字符串str在由N个子字符串组成的字符串列表strlist中,则返回1到N范围内的值.
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings.
字符串列表是由由,"
字符分隔的子字符串组成的字符串.如果第一个参数是常量字符串,第二个参数是SET类型的列,则将优化 FIND_IN_SET()
函数以使用位算术.
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.
如果str不在strlist中或strlist为空字符串,则返回0.如果任一参数为NULL,则返回NULL.
Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL.
如果第一个参数包含逗号(,")字符,则此功能将无法正常工作.
$this->db->where("FIND_IN_SET( '$post_period' , period) ");
这篇关于检查条件(以逗号分隔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!