使用mysql查询在单个列中的两列值

时间:2016-01-02 10:38:03

标签: mysql

我收到不同格式的输出,这是我的查询:

SELECT
  COUNT(lead_id) AS `leads`,
  Month(FROM_UNIXTIME(`created_at`)) AS `Month`
FROM `lead`  
WHERE YEAR(FROM_UNIXTIME(`created_at`)) = YEAR(CURDATE())
GROUP BY Month(FROM_UNIXTIME(`created_at`)); 

输出:

Month
312c31
322c31
332c31
342c31
352c31
362c31
372c31
382c31
392c31
31302c31
31312c31
31322c31

必填项:

month
1,1
2,23
3,4
5,6
6,34
7,76
8,2
9,3
10,5
11,4
12,1

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

SELECT
  CAST(
      CONCAT(
          MONTH(FROM_UNIXTIME(`created_at`)), 
          ',',
          COUNT(lead_ID)
      ) AS CHAR
  ) AS 'Month'
FROM lead
WHERE `created_at` BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-01-01")) AND UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-12-31"))
GROUP BY MONTH(FROM_UNIXTIME(`created_at`))

答案 1 :(得分:0)

我得到了一个回答,谢谢所有回复你的回复也是一个有用的信息,这里是我的查询

SELECT
    *,
    concat(leads, ",", month)as leadss 
FROM (
    SELECT
        COUNT(lead_id) as 'leads,
        Month(FROM_UNIXTIME(`created_at`)) AS `Month`
    FROM `lead`
    WHERE YEAR(FROM_UNIXTIME(`created_at`)) = YEAR(CURDATE())
    GROUP BY Month(FROM_UNIXTIME(`created_at`))
)AS ab