duanhao5038 2015-04-22 14:34
浏览 75

避免重复值并在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..?

  • 写回答

3条回答

  • duanhuang2150 2015-04-22 14:42
    关注

    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;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统