关键字中SQL内部的条件


Condition inside SQL in keyword

这是我的查询

$qry="select * from table where colnam in ($int1, $int2)";

colname值可以是1,4,3,2

例如

if $int1=2;

我想选择1, 2 and 3

if $int1=3;

我想选择2, 3 and 4

有可能吗?

将查询重写为:

$qry="select * from table where colnam BETWEEN $int1-- AND $int1++ ";

根本不需要$int2。只需在$int1中指定值2 or 3即可。

使用此代码:

$col= ($int1-1).",".($int1).",".($int1+1);
$qry="select * from table where colnam in ($col)";
$qry="select * from table where colnam in ($int1,int1++,int1--)";

在sql查询中,您不能编写条件,因此使用以下代码进行查询,

if($int1 == 2){
  $int1 = 1,2,3;
}
elseif($int1 == 3){
  $int1 = 2,3,4;
}

然后执行查询,

$qry="select * from table where colnam in ($int1, $int2)";