ORDER BY RAND的一种比较好的方案][CI]

来源:互联网 发布:algorithm基础算法 编辑:程序博客网 时间:2024/06/08 18:58

$aid = $this->session->userdata('aid');

$num = $this->data->num;

$this->db->select_max('rid');

$query = $this->db->get(TABLE_NAME_ROLE);

$max = $query->row()->rid;


$this->db->select('*');

$this->db->where('aid',$aid);

$this->db->where('rid >',$max.' * RAND()',FALSE);

$query = $this->db->get(TABLE_NAME_ROLE,$num);


类似:

SELECT * FROM users WHERE userId >= ((SELECTMAX(userId) FROM users)-(SELECT MIN(userId) FROM users)) * RAND() +(SELECT MIN(userId) FROM users) LIMIT 10


0 0
原创粉丝点击