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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算