避免重复值并在mysql数据库中的相同行中获取其他数据集

I have a database table that contain several projects. I want to show each project in a tab view. so I want to get each project name once and fetch other details under that project name. My table looks like below.

+-------------+----------+--------------+---------+-------------+
| activity_id | activity | project_name | user_id | description |
+-------------+----------+--------------+---------+-------------+
|       1     |    A     | New Project  |    5    |   Project   |
|       2     |    B     | New Project  |    5    |   Project   |
|       3     |    C     | New Project  |    5    |   Project   |
|       4     |    D     | New Project  |    5    |   Project   |
|       5     |    E     | New Project  |    5    |   Project   |
|       6     |    A     | Old Project  |    5    |   New one   |
|       7     |    B     | Old Project  |    5    |   New one   |
|       8     |    C     | Old Project  |    5    |   New one   |
|       9     |    A     | Another One  |    5    |   Test 01   |
|      10     |    B     | Another One  |    5    |   Test 01   |
|      11     |    C     | Another One  |    5    |   Test 01   |
|      13     |    D     | Another One  |    5    |   Test 01   |
+-------------+----------+--------------+---------+-------------+

Is there a way to get this table like below array?

['New Project'] =>
      [0] =>
          ['activity_Id']   =>   '1'
          ['activity']      =>   'A'
          ['user_id']       =>   '5'
          ['description']   =>   'project'
      [1] =>
          ['activity_Id']   =>   '2'
          ['activity']      =>   'B'
          ['user_id']       =>   '5'
          ['description']   =>   'Project'

and like vice..?

dpd20130
dpd20130 我试过Distinct..不起作用..你可以提供如何使用它的方式吗?
5 年多之前 回复
dongwen9051
dongwen9051 嗯......GROUPBY?DISTINCT也。
5 年多之前 回复

3个回答

This would do the trick:

$stm = $pdo->query('SELECT * FROM tablename');
while( $row = $stm->fetch(PDO::FETCH_ASSOC) )
{
    $info = array(
        'activity_id' => $row['activity_id'],
        'activity' => $row['activity'],
        'user_id' => $row['user_id'],
        'description' => $row['description']
    );
    if( array_key_exists($row['project_name'], $array) && is_array($array[$row['project_name']]) )
    {
        $array[$row['project_name']][] = $info;
    }
    else
    {
        $array[$row['project_name']] = array();
        $array[$row['project_name']][] = $info;
    }
}
doujie7346
doujie7346 不熟悉CI和活动记录,但是如果你可以将结果作为关联数组或对象得到它应该可以正常工作。
5 年多之前 回复
dtf1111
dtf1111 我正在使用CI框架..这可能与活动记录有关吗?
5 年多之前 回复



两个查询将轻松完成,
第一次查询以根据第一个查询项目名称获取不同的项目名称和第二个查询数据。 / p>

在此结果上从表
for循环中查询选择distinct project_name
query select table_Id,activity,user_id,table中的描述,其中project_name =来自第一个查询的结果
for此结果的结尾
make数组 如你所愿</ p>
</ div>

展开原文

原文

two queries will do it easily, first query to fetch distinct project name and from second query data according to first query project name.

query select distinct project_name from table for loop on this result query select activity_Id, activity, user_id, description from table where project_name = result from first query for loop on this result make array as you wish



不,AFAIK,你不能直接用活动记录 -now在CI3中称为查询生成器这种嵌套 - </ em>(但你可能用ORM实现它,像CakePHP和Laravel这样的框架有自己的实现)。</ p>

唯一的方法是创建一个新数组并迭代 您从模型中获得的结果数组,以便构建您需要的结构。</ p>

您可以在此处检查有关查询生成器的CI3文档: http://www.codeigniter.com/userguide3/database/query_builder.html </ p>
</ div>

展开原文

原文

No, AFAIK, you can't get this kind of nesting directly with active record -now in CI3 known as Query Builder- (but you probably could achieve it with ORM, frameworks like CakePHP and Laravel have their own implementations).

The only way is to create a new array and iterate over the result array you got from the model in order to build the structure you need.

You can check CI3 docs about Query Builder here: http://www.codeigniter.com/userguide3/database/query_builder.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问