根据过去30天的平均值对两张表进行排序

时间:2015-08-22 12:15:54

标签: php mysql

我已经在解决我的编码挑战方面长时间转向你的见解。我终于难倒了,经过12个小时的尝试,我必须向自己的问题转​​向大师。我很欣赏你提供的任何方向。

我有两个mysql表; POLLS表有一个民意调查列表,DATA表是那些民意调查中的数据。我试图按照过去30天内平均值确定的顺序列出候选人。

表POLLS

poll_id  |  poll_name  |  poll_date
_________|_____________|___________
6        |   poll 6    | 2015-08-22
5        |   poll 5    | 2015-08-14
4        |   poll 4    | 2015-08-09
3        |   poll 3    | 2015-08-02
2        |   poll 2    | 2015-07-28
1        |   poll 1    | 2015-07-20

表格数据

data_id | data_key | candidate | percent
________|__________|___________|________
1       |   1      |   Joe     |  18
2       |   1      |   Sue     |  19
3       |   1      |   Joy     |  15
4       |   1      |   Tim     |  16
5       |   2      |   Joe     |  20
6       |   2      |   Sue     |  19
7       |   2      |   Joy     |  19
8       |   2      |   Tim     |  22
9       |   3      |   Joe     |  14
10      |   3      |   Sue     |  16
11      |   3      |   Joy     |  21
12      |   3      |   Tim     |  15
13      |   4      |   Joe     |  14
14      |   4      |   Sue     |  15
15      |   4      |   Joy     |  20
16      |   4      |   Tim     |  15
17      |   5      |   Joe     |  17
18      |   5      |   Sue     |  14
19      |   5      |   Joy     |  15
20      |   5      |   Tim     |  16
21      |   6      |   Joe     |  17
22      |   6      |   Sue     |  19
23      |   6      |   Joy     |  16
24      |   6      |   Tim     |  18

这是我正在寻找的表格结果。它包含表DATA中的民意调查百分比,但候选人根据过去30天的平均百分比列出。

candidate | poll 6 | poll 5 | poll 4 | poll 3 | poll 2
__________|________|________|________|________|_______
Joy       | 16     | 15     | 20     | 21     | 19
Tim       | 18     | 16     | 15     | 15     | 22
Sue       | 19     | 14     | 15     | 16     | 19
Joe       | 17     | 17     | 14     | 14     | 20

我混淆了,我甚至不知道从哪里开始。我已经尝试了很多可能性,但只能根据候选人的名字让他们订购。

感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:0)

例如......

DROP TABLE IF EXISTS polls;

CREATE TABLE polls
(poll_id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,poll_name  VARCHAR(12) NOT NULL
,poll_date DATE NOT NULL
);

INSERT INTO polls VALUES
(6        ,'poll 6','2015-08-22'),
(5        ,'poll 5','2015-08-14'),
(4        ,'poll 4','2015-08-09'),
(3        ,'poll 3','2015-08-02'),
(2        ,'poll 2','2015-07-28'),
(1        ,'poll 1','2015-07-20');

DROP TABLE IF EXISTS data;

CREATE TABLE data
(data_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,poll_id INT NOT NULL
,candidate VARCHAR(12) NOT NULL
,percent INT NOT NULL
);

INSERT INTO data VALUES
( 1,1,'Joe',18),
( 2,1,'Sue',19),
( 3,1,'Joy',15),
( 4,1,'Tim',16),
( 5,2,'Joe',20),
( 6,2,'Sue',19),
( 7,2,'Joy',19),
( 8,2,'Tim',22),
( 9,3,'Joe',14),
(10,3,'Sue',16),
(11,3,'Joy',21),
(12,3,'Tim',15),
(13,4,'Joe',14),
(14,4,'Sue',15),
(15,4,'Joy',20),
(16,4,'Tim',15),
(17,5,'Joe',17),
(18,5,'Sue',14),
(19,5,'Joy',15),
(20,5,'Tim',16),
(21,6,'Joe',17),
(22,6,'Sue',19),
(23,6,'Joy',16),
(24,6,'Tim',18);

SELECT d.candidate
     , p.poll_name
     , d.percent
     , 30_day_avg
  FROM polls p
  JOIN data d
    ON d.poll_id = p.poll_id
  JOIN (SELECT candidate, AVG(percent) 30_day_avg FROM polls p JOIN data d ON d.poll_id = p.poll_id WHERE poll_date >= CURDATE() - INTERVAL 30 day GROUP BY candidate) x
    ON x.candidate = d.candidate
 WHERE p.poll_date >= CURDATE() - INTERVAL 30 day
 ORDER
    BY 30_day_avg DESC, candidate, p.poll_id DESC;
+-----------+-----------+---------+------------+
| candidate | poll_name | percent | 30_day_avg |
+-----------+-----------+---------+------------+
| Joy       | poll 6    |      16 |    18.2000 |
| Joy       | poll 5    |      15 |    18.2000 |
| Joy       | poll 4    |      20 |    18.2000 |
| Joy       | poll 3    |      21 |    18.2000 |
| Joy       | poll 2    |      19 |    18.2000 |
| Tim       | poll 6    |      18 |    17.2000 |
| Tim       | poll 5    |      16 |    17.2000 |
| Tim       | poll 4    |      15 |    17.2000 |
| Tim       | poll 3    |      15 |    17.2000 |
| Tim       | poll 2    |      22 |    17.2000 |
| Sue       | poll 6    |      19 |    16.6000 |
| Sue       | poll 5    |      14 |    16.6000 |
| Sue       | poll 4    |      15 |    16.6000 |
| Sue       | poll 3    |      16 |    16.6000 |
| Sue       | poll 2    |      19 |    16.6000 |
| Joe       | poll 6    |      17 |    16.4000 |
| Joe       | poll 5    |      17 |    16.4000 |
| Joe       | poll 4    |      14 |    16.4000 |
| Joe       | poll 3    |      14 |    16.4000 |
| Joe       | poll 2    |      20 |    16.4000 |
+-----------+-----------+---------+------------+

遗憾的是,PHP不是我的强项,但是这里有一个例子,说明如何在PHP中转换数组(从而导致这种结果)...

<?php

require('path/to/connection/stateme.nts');

$query = "
SELECT d.candidate
     , p.poll_name
     , d.percent
     , 30_day_avg
  FROM polls p
  JOIN data d
    ON d.poll_id = p.poll_id
  JOIN (SELECT candidate, AVG(percent) 30_day_avg FROM polls p JOIN data d ON d.poll_id = p.poll_id WHERE poll_date >= CURDATE() - INTERVAL 30 day GROUP BY candidate) x
    ON x.candidate = d.candidate
 WHERE p.poll_date >= CURDATE() - INTERVAL 30 day
 ORDER
    BY 30_day_avg DESC, candidate, p.poll_id DESC;
";

$result = mysqli_query($conn,$query);

$old_array = array();

while($row = mysqli_fetch_assoc($result)){

$old_array[] = $row;

}

$new_array = array();

foreach( $old_array as $v )
{
    if(!isset( $new_array[$v["candidate"]][$v["poll_name"]]))
    {
       $new_array[$v["candidate"]][($v["poll_name"])] = $v["percent"];
    }
}

print_r($new_array);

?> 

输出:

Array
(
    [Joy] => Array
        (
            [poll 6] => 16
            [poll 5] => 15
            [poll 4] => 20
            [poll 3] => 21
            [poll 2] => 19
        )

    [Tim] => Array
        (
            [poll 6] => 18
            [poll 5] => 16
            [poll 4] => 15
            [poll 3] => 15
            [poll 2] => 22
        )

    [Sue] => Array
        (
            [poll 6] => 19
            [poll 5] => 14
            [poll 4] => 15
            [poll 3] => 16
            [poll 2] => 19
        )

    [Joe] => Array
        (
            [poll 6] => 17
            [poll 5] => 17
            [poll 4] => 14
            [poll 3] => 14
            [poll 2] => 20
        )

)

希望从那里你可以弄清楚如何使用html和/或css构建一个漂亮的表。