我想声明一个表中的3个随机行用作簇质心
tabel名称是data\u 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;
关于php - 如何从表中获取随机行值并将其存储在数组中以分别调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56880129/