如何从表中获取随机行值并将其存储在数组中以分别调用

只是一个废话

我想从表中声明3个随机行以用作聚类质心

表格名称为data_kuesioner,包含列variable1,variable2,variable3,variable4,variable5

如何选择3个不同的随机行和值,然后调用其单个值,例如:

$centroid1cluster1 = (random row 1 variable 1)
$centroid2cluster1 = (random row 1 variable 2)
$centroid3cluster1 = (random row 1 variable 3)
$centroid4cluster1 = (random row 1 variable 4)
$centroid5cluster1 = (random row 1 variable 5)

$centroid1cluster2 = (random row 2 variable 1)
$centroid2cluster2 = (random row 2 variable 2)
$centroid3cluster2 = (random row 2 variable 3)
$centroid4cluster2 = (random row 2 variable 4)
$centroid5cluster2 = (random row 2 variable 5)

 $centroid1cluster3 = (random row 3 variable 1)
$centroid2cluster3 = (random row 3 variable 2)
$centroid3cluster3 = (random row 3 variable 3)
$centroid4cluster3 = (random row 3 variable 4)
$centroid5cluster3 = (random row 3 variable 5)

等等

赫曼特

要获得准确的3行和随机行,可以在下面的查询中使用

$sql = $this->db->query("select variable1,variable2,variable3,variable4,variable5 from data_kuesioner order by RAND() limit 3");
echo'<pre>';print_r($sql->result());die;

与方法链接

$this->db->select("variable1,variable2,variable3,variable4,variable5");
$this->db->from('data_kuesioner');
//$this->db->order_by('variable1','RANDOM');
//or
$this->db->order_by('rand()');
$this->db->limit(3);
$query = $this->db->get();
if ($query->num_rows() > 0) {
    $result = $query->result_array();
    $j = 1;
    $dynamicVariable = array();
    foreach ($result as $row) {
       $dynamicVariable['centroid1cluster'.$j]= $row['variable1'];
       $dynamicVariable['centroid2cluster'.$j] = $row['variable2'];
       $dynamicVariable['centroid3cluster'.$j] = $row['variable3'];
       $dynamicVariable['centroid4cluster'.$j] = $row['variable4'];
       $dynamicVariable['centroid5cluster'.$j] = $row['variable5'];
       $j++;
    }
}
echo'<pre>';print_r($dynamicVariable);die;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章