假设我有这么一个数组 score[10] ,这个数组里面内容如下 score[0]=78 ,score[1]=80,score[2]=65,........那么我怎么使用sql语句来取得分数是在这数组所包含分数记录范围的记录,我记得sql语句有着么一种写法,就是select score from table where score in ........,但怎么把数组这些数据应用到这个语句里面,最好的方法是怎么样的?

解决方案 »

  1.   

    1。如果数组大小是一定的,如10,可用10个参数:
       declare @score0 int,
       declare @score1 int,
       declare @score2 int,
       declare @score3 int,
       declare @score4 int,
       declare @score5 int,
       declare @score6 int,
       declare @score7 int,
       declare @score8 int,
       declare @score9 int
      带入,速度会快些。
      select score from table where score =@score0
      union all
      select score from table where score =@score1
      union all
      select score from table where score =@score2
      union all
      select score from table where score =@score3
      union all
      select score from table where score =@score4
      union all
      select score from table where score =@score5
      union all
      select score from table where score =@score6
      union all
      select score from table where score =@score7
      union all
      select score from table where score =@score8
      union all
      select score from table where score =@score92。如果数组大小不定,可将数组数据用逗号分割后赋给参数,如'78,80,65'赋给@score :
      declare @score varchar(2000)
      declare @sql varchar(2000)
      set @sql = 'select score from table where score in ('''+@score+''')'
      exec(@sql)
      

  2.   


    //PHP Edition
    <?
     $score=implode(",",$score);
     $sql="select score from table where score in ".$score;
    //执行查询......
    ?>
      

  3.   

    set @sql = 'select score from table where score in ('+@score+')'
      exec(@sql)