选择包含日期和文章点的行(无关系)

时间:2013-02-18 19:40:52

标签: mysql sql database pchart

我有以下表格:

制品

+----+-------------+-----------------------------+--------------+
| ID | ID_group_AG | Title                       | Date_publish |
+----+-------------+-----------------------------+--------------+
|  1 |          10 | O obrotach sfer niebieskich | 2009-05-07   |
|  2 |          11 | Technologia betonu          | 2011-03-21   |
|  3 |          12 | test                        | 2008-01-13   |
+----+-------------+-----------------------------+--------------+

雇员

+----+-----------+-----------+
| ID | Name      | Surname   |
+----+-----------+-----------+
|  1 | Andrzej   | Gacek     |
|  2 | Leszek    | Ksiazek   |
|  3 | Krzysztof | Skibinski |
|  4 | Andrzej   | Inny      |
+----+-----------+-----------+

articlesGroup

+----+----------+---------------+----------------+
| ID | ID_group | ID_employee   | Points         |
+----+----------+---------------+----------------+
|  1 |       10 |             1 |              3 |
|  2 |       10 |             3 |              3 |
|  3 |       11 |             1 |              2 |
|  4 |       11 |             2 |              2 |
|  5 |       11 |             4 |              2 |
|  6 |       12 |             4 |              6 |
+----+----------+---------------+----------------+

以下关系:

articles.ID_group_AG => articlesGroup.ID_group

articlesGroup.ID_employee => employee.ID

我需要做的是打印与员工,文章和发布日期相关的所有文章点,所以我使用了以下查询:

SELECT
  p.Name,
  p.Surname,
  a.Date_publish,
  ag.Points
FROM
  employee p,
  articles a,
  articlesGroup ag
WHERE
  (ag.ID_group = a.ID_group_AG) AND
  (ag.ID_employee = p.ID)

我得到了:

+-----------+-----------+--------------+----------------+
| Name      | Surname   | Date_publish | Points         |
+-----------+-----------+--------------+----------------+
| Andrzej   | Gacek     | 2009-05-07   |              3 |
| Andrzej   | Gacek     | 2011-03-21   |              2 |
| Leszek    | Ksiazek   | 2011-03-21   |              2 |
| Krzysztof | Skibinski | 2009-05-07   |              3 |
| Andrzej   | Inny      | 2011-03-21   |              2 |
| Andrzej   | Inny      | 2008-01-13   |              6 |
+-----------+-----------+--------------+----------------+

现在让我们解决问题:)

我正在使用pChart库制作图表。

我想在X轴上放置Y轴点,关于每个员工的所有日期。

员工“Andrzej Gacek”的Point数组将是:[3,2]

员工“Krzysztof Skibinski”将是:[3]

和日期数组(已排序):[“2008-01-13”,“2009-05-07”,“2011-03-21”]

我需要为员工Points数组添加零点。对于“Andrzej Gacek”阵列应该看起来像:[0,3,2] 因此Point将与日期相关联。

如何形成查询以将零添加到点,以便查询的输出如下所示:

+-----------+-----------+--------------+----------------+
| Name      | Surname   | Date_publish | Points         |
+-----------+-----------+--------------+----------------+
| Andrzej   | Gacek     | 2009-05-07   |              3 |
| Andrzej   | Gacek     | 2011-03-21   |              2 |
| Andrzej   | Gacek     | 2008-01-13   |              0 |
| Leszek    | Ksiazek   | 2011-03-21   |              2 |
| Leszek    | Ksiazek   | 2009-05-07   |              0 |
| Leszek    | Ksiazek   | 2008-01-13   |              0 |
| Krzysztof | Skibinski | 2009-05-07   |              3 |
| Krzysztof | Skibinski | 2011-03-21   |              0 |
| Krzysztof | Skibinski | 2008-01-13   |              0 |
| Andrzej   | Inny      | 2011-03-21   |              2 |
| Andrzej   | Inny      | 2008-01-13   |              6 |
| Andrzej   | Inny      | 2009-05-07   |              0 |
+-----------+-----------+--------------+----------------+

1 个答案:

答案 0 :(得分:3)

您必须创建笛卡尔积以获得您要查找的结果。我使用CROSS JOIN来获取每位员工的日期。

尝试一下:

SELECT DISTINCT E.Name,
  E.SurName,
  a.Date_Publish, 
  IFNULL(AG.Points,0) Points
FROM Articles A CROSS JOIN
  Employee E LEFT JOIN
  ArticlesGroup AG ON ag.ID_group = a.ID_group_AG 
    AND E.Id = ag.ID_employee

这是SQL Fiddle