Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter如何不选择重复的id_Php_Database_Arrays_Codeigniter - Fatal编程技术网

Php Codeigniter如何不选择重复的id

Php Codeigniter如何不选择重复的id,php,database,arrays,codeigniter,Php,Database,Arrays,Codeigniter,我有一个数据库,如果我自己创建数组,那么获取的数组看起来像这样 <?php $array[] = array('id' => 1, 'message' => 'test'); $array[] = array('id' => 1, 'message' => 'test'); $array[] = array('id' => 2, 'message' => 'test'); $array[] = array('id' =&

我有一个数据库,如果我自己创建数组,那么获取的数组看起来像这样

<?php

    $array[] = array('id' => 1, 'message' => 'test');
    $array[] = array('id' => 1, 'message' => 'test');
    $array[] = array('id' => 2, 'message' => 'test');
    $array[] = array('id' => 5, 'message' => 'test');
    $array[] = array('id' => 1, 'message' => 'test');
    $array[] = array('id' => 8, 'message' => 'test');
    $array[] = array('id' => 5, 'message' => 'test');
    $array[] = array('id' => 1, 'message' => 'test');

?>

如您所见,有几个ID为1的数组,我想选择最后5项,但如果有重复项,我想跳过它,然后继续。有什么简单的解决办法吗

您应该使用如下查询:


从您的_表中选择DISTINCT id,message LIMIT 0,5

您应该使用如下查询:

从您的_表中选择DISTINCT id,message LIMIT 0,5

MySQL方法: 请参考本文@w3schools:

如果您坚持在使用MySQL的首选方法之外进行此操作,则可以使用PHPS array_独特的函数

MySQL方法: 请参考本文@w3schools:

如果您坚持在使用MySQL的首选方法之外进行此操作,则可以使用PHPS array_独特的函数

您正在使用Active Record类吗

如果是,请在获取结果之前,将此行添加到查询中:

$this->db->distinct();
作为当前和未来的参考,CI拥有并因其出色的文档而受到赞扬! 这里也值得一看:

如果不使用Active Record类,请按照上面的建议执行:。

是否使用Active Record类

如果是,请在获取结果之前,将此行添加到查询中:

$this->db->distinct();
作为当前和未来的参考,CI拥有并因其出色的文档而受到赞扬! 这里也值得一看:


如果不使用Active Record类,请按照上面的建议执行:。

是否可以显示数据库查询。。。。可能需要group by/distinctGo到源,更改查询并让它选择distinct(如果可能)。否则,您将需要遍历它并删除您不想要的内容。您可以显示db查询。。。。可能需要group by/distinctGo到源,更改查询并让它选择distinct(如果可能)。否则,您将需要通过它循环并删除您不想要的内容
$this->db->distinct();