0

I have a table "events" like this

   id   |  user_id   |   date     |  is_important
---------------------------------------------------
   1    |     3      | 01/02/2012 |       0
   1    |     3      | 01/02/2012 |       1
   1    |     3      | 01/02/2011 |       1
   1    |     3      | 01/02/2011 |       1
   1    |     3      | 01/02/2011 |       0

Basically, what I need to get is this:

(for the user_id=3)

   year   |  count   |   count_importants
--------------------------------------------
  2012    |    2     |          1
  2011    |    3     |          2

I've tried this:

SELECT YEAR(e1.date) as year,COUNT(e1.id) as count_total, aux.count_importants
FROM events e1 
LEFT JOIN 
(
  SELECT YEAR(e2.date) as year2,COUNT(e2.id) as count_importants
  FROM `events` e2
  WHERE e2.user_id=18 
  AND e2.is_important = 1
  GROUP BY year2
) AS aux ON aux.year2 = e1.year
WHERE e1.user_id=18 
GROUP BY year

But mysql gives me an error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'aux ON aux.year2 = e1.year WHERE e1.user_id=18 GROUP BY year LIMIT 0, 30' at line 10

And i've run out of ideas to make this query u_Uº. Is it possible to do this using only one query??

Thanks in advance

4

2 回答 2

1

Edit: I think I over-complicated things. Can't you just do this in a simple query?

SELECT 
    YEAR(`year`) AS `year`, 
    COUNT(`id`) AS `count`, 
    SUM(`is_important`) AS `count_importants`
FROM `events`
WHERE user_id = 18
GROUP BY YEAR(`year`)

Here's the big solution that adds summaries :)

Consider using MySQL GROUP BY ROLLUP. This will basically do a similar job to a normal GROUP BY, but will add rows for the summaries too.

In the example below, you see two records for Finland in 2000, for £1500 and £100, and then a row with the NULL product with the combined value of £1600. It also adds NULL rollup rows for each dimension grouped by.

From the manual:

SELECT year, country, product, SUM(profit)
FROM sales
GROUP BY year, country, product WITH ROLLUP

+------+---------+------------+-------------+
| year | country | product    | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer   |        1500 |
| 2000 | Finland | Phone      |         100 |
| 2000 | Finland | NULL       |        1600 |
| 2000 | India   | Calculator |         150 |
| 2000 | India   | Computer   |        1200 |
| 2000 | India   | NULL       |        1350 |
| 2000 | USA     | Calculator |          75 |
| 2000 | USA     | Computer   |        1500 |
| 2000 | USA     | NULL       |        1575 |
| 2000 | NULL    | NULL       |        4525 |
| 2001 | Finland | Phone      |          10 |
| 2001 | Finland | NULL       |          10 |
| 2001 | USA     | Calculator |          50 |
| 2001 | USA     | Computer   |        2700 |
| 2001 | USA     | TV         |         250 |
| 2001 | USA     | NULL       |        3000 |
| 2001 | NULL    | NULL       |        3010 |
| NULL | NULL    | NULL       |        7535 |
+------+---------+------------+-------------+

Here's an example the specifically matches your situation:

SELECT year(`date`) AS `year`, COUNT(`id`) AS `count`, SUM(`is_important`) AS `count_importants`
FROM new_table
GROUP BY year(`date`) WITH ROLLUP;
于 2012-05-11T09:49:56.193 回答
0

The alias year - year(e1.date) AS year is not visible in JOIN ON clause. Try to use this condition -

...
LEFT JOIN
(
...
) ON aux.year2 = year(e1.date) -- e1.year --> year(e1.date)
...
于 2012-05-11T09:43:29.040 回答