在Month上获取记录 - mysql

时间:2014-06-19 07:25:01

标签: mysql

Fiddle

查询,

SELECT
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=1,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '1',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=2,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '2',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=3,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '3',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=4,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '4',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=5,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '5',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=6,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '6',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=7,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '7',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=8,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '8',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=9,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '9',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=10,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '10',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=11,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '11',
  IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=12,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '12'
FROM `tyr_profile_view` p
WHERE p.user_id != '59'
    AND p.`user_id` != 0
    AND YEAR(FROM_UNIXTIME(p.created_timestamp)) = YEAR(CURDATE())

记录显示,

enter image description here

以上查询的输出

|  1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|----|---|---|---|---|---|---|---|---|----|----|----|
| 18 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 | 

以上数据清楚你,我的问题是什么,但如果你仍然不理解我的问题请随时问我。

P.S。我想要一种能够准确显示结果的正确方法。这样做有效,但结果不合适。

4 个答案:

答案 0 :(得分:1)

您的问题是因为您为每条记录选择了COUNT。这不是IF的工作方式。相反,您应该在外部范围内进行聚合,而不是:

IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=1,COUNT(MONTH(FROM_UNIXTIME(p.created_timestamp))),0) AS '1'

DO

COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=1, 1, NULL)) AS '1'

答案 1 :(得分:1)

你可以试试

SELECT
sum(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=1,1,0)) AS '1',
sum(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=2,1,0)) AS '2',
sum(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=3,1,0)) AS '3',
sum(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=4,1,0)) AS '4',
sum(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=5,1,0)) AS '5',
sum(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=6,1,0)) AS '6',
..........................
FROM `tyr_profile_view` p
WHERE p.user_id != '59'
    AND p.`user_id` != 0
    AND YEAR(FROM_UNIXTIME(p.created_timestamp)) = YEAR(CURDATE())

因此,它将通过检查if条件

中的月份值来获得sum()

<强> DEMO

答案 2 :(得分:1)

你可以这样做

Fiddle

查询

SELECT
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=1, 1, NULL)) AS '1',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=2, 1, NULL)) AS '2',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=3, 1, NULL)) AS '3',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=4, 1, NULL)) AS '4',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=5, 1, NULL)) AS '5',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=6, 1, NULL)) AS '6',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=7, 1, NULL)) AS '7',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=8, 1, NULL)) AS '8',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=9, 1, NULL)) AS '9',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=10, 1, NULL)) AS '10',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=11, 1, NULL)) AS '11',
    COUNT(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=12, 1, NULL)) AS '12'
FROM `tyr_profile_view` p
WHERE p.user_id != '59'
    AND p.`user_id` != 0
    AND YEAR(FROM_UNIXTIME(p.created_timestamp)) = YEAR(CURDATE())

输出

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|----|----|----|
| 1 | 0 | 0 | 3 | 6 | 8 | 0 | 0 | 0 |  0 |  0 |  0 |

注意:创建此查询为Alma Do Suggested

完全要求: enter image description here

答案 3 :(得分:1)

尝试使用SUM合并IF

SELECT
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=1, 1, 0)) AS 'January',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=2, 1, 0)) AS 'Feburary',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=3, 1, 0)) AS 'March',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=4, 1, 0)) AS 'April',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=5, 1, 0)) AS 'May',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=6, 1, 0)) AS 'June',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=7, 1, 0)) AS 'July',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=8, 1, 0)) AS 'Augest',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=9, 1, 0)) AS 'September',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=10, 1, 0)) AS 'October',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=11, 1, 0)) AS 'November',
  SUM(IF(MONTH(FROM_UNIXTIME(p.created_timestamp))=12, 1, 0)) AS 'December'
FROM `tyr_profile_view` p
WHERE p.user_id != '59'
    AND p.`user_id` != 0
    AND YEAR(FROM_UNIXTIME(p.created_timestamp)) = YEAR(CURDATE())

输出:


JANUARY FEBURARY    MARCH   APRIL   MAY JUNE    JULY    AUGEST  SEPTEMBER   OCTOBER NOVEMBER    DECEMBER
1       0           0       3       6   8       0       0       0           0        0           0

<强> Fiddle Demo