动态查询分组特定字段

时间:2017-01-28 19:12:01

标签: php mysql angularjs

我正在尝试在前端显示一个表格。应用程序用AngularJS编写,PHP用MySQL编写。

我的表格如下:

+-----------+--------+--------+
| id_person | id_car | value  |
+-----------+--------+--------+
| 1         |   1    |   1000 |
| 1         |   2    |   2000 |
| 2         |   1    |   2200 |
| 2         |   2    |   1100 |
| 3         |   3    |   3000 |
+-----------+--------+--------+

我需要以这种格式在前端显示一个表:

+-----------+--------+--------+---------+
|           | Car 1  | Car 2  |  Car 3  |
+-----------+--------+--------+---------|
| Person 1  |  1000  |   2000 |    0    |
| Person 2  |  2200  |   1100 |    0    |
| Person 3  |   0    |   0    |  3000   |
+-----------+--------+--------+---------+

每个人的价值都会出现在汽车领域。 如果value为null或不存在,则它将显示0或作为空值。

任何线索?

感谢。

2 个答案:

答案 0 :(得分:1)

从服务器返回JSON,如

[
{
  "person": {"id":1, "name":"Sam"},
  "cars": [
    {"id":1, "name":"Ford", "value": 1000},
    {"id":1, "name":"Audi", "value": 1200}
  ]
},
//....

]

答案 1 :(得分:1)

您可以将SUM与GROUP BY结合使用,以根据需要旋转表格。

创建表格/插入

SELECT 
   CONCAT('Person ', cars.id_person) "Person"
 , SUM(CASE WHEN cars.id_car = 1 THEN cars.value ELSE 0 END) "Car 1"
 , SUM(CASE WHEN cars.id_car = 2 THEN cars.value ELSE 0 END) "Car 2"
 , SUM(CASE WHEN cars.id_car = 3 THEN cars.value ELSE 0 END) "Car 3"
FROM
 cars
GROUP BY
 cars.id_person 
ORDER BY
 cars.id_person ASC

<强>查询

Person    Car 1   Car 2   Car 3   
--------  ------  ------  --------
Person 1  1000    2000    0       
Person 2  2200    1100    0       
Person 3  0       0       3000    

<强>结果

SELECT 
   CONCAT('Person ', cars.id_person) "Person"
 , SUM(CASE WHEN cars.id_car = 1 THEN cars.value ELSE NULL END) "Car 1"
 , SUM(CASE WHEN cars.id_car = 2 THEN cars.value ELSE NULL END) "Car 2"
 , SUM(CASE WHEN cars.id_car = 3 THEN cars.value ELSE NULL END) "Car 3"
FROM
 cars
GROUP BY
 cars.id_person 
ORDER BY
 cars.id_person ASC

请参阅演示http://sqlfiddle.com/#!9/65bf69/1

或者对于NULLS

<强>查询

Person    Car 1   Car 2   Car 3   
--------  ------  ------  --------
Person 1  1000    2000    (NULL)  
Person 2  2200    1100    (NULL)  
Person 3  (NULL)  (NULL)  3000    

<强>结果

<nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed"
                  data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">OSV</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Realizar Busqueda</a></li>
          </ul>
        </div>
      </div>
    </nav>

<div class="container-fluid">
    <div class="container">
        <div class="row">
            <div class="col-md-offset-4 col-md-4">
                <form  action="{% url 'buscador_ingresar_calle' %}" method="get">
                    {{ form.non_field_errors }}
                    {% for field in form %}
                        <div class="form-group">
                            {{ field.errors }}
                                <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                            {{ field }}
                        </div>
                    {% endfor %}
                        <button type="submit" class="btn btn-default btn-blue btn-lg btn-block">Enviar</button>
                </form>
            </div>
        </div>
    </div>
</div>

参见演示http://sqlfiddle.com/#!9/65bf69/2