2015-03-30 118 views
0

我对我的数据库有一个查询,这些数据库对他们完成的日期进行了采访,我只需要能够仅拖动当前月份和年份的采访并计数它们然后将他们与接受采访的人分组。无法从数据库中获得当前的月份和年份

PHP

$time = time(); 

QUERY

select count(i.interv_id) as cnt, u.user_name, year(from_unixtime($time)) as year, month(from_unixtime($time)) as month 
from support.support_interviews as i 
left join support.support_logs as l on l.log_id = i.log_id 
left join support.support_users as u on u.user_id = l.user_id 
group by u.user_name, month, year 
order by cnt desc 

此查询工作正常,并且我得到了我想要什么,但我把它在表中统计的所有数据。

表结构

CREATE TABLE support_interviews (
    interv_id int(11) NOT NULL AUTO_INCREMENT, 
    log_id int(11) NOT NULL, 
    interv_date int(11) NOT NULL, 
    interv_url varchar(255) NOT NULL, 
    PRIMARY KEY (interv_id) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=36 ; 

DATA

INSERT INTO `support_interviews` VALUES(1, 1, 1413849800, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(2, 1, 1413849800, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(3, 1, 1413849800, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(4, 2, 1413936200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(5, 2, 1413936200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(6, 3, 1414973000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(7, 3, 1414973000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(8, 3, 1414973000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(9, 3, 1414973000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(10, 4, 1415750600, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(11, 4, 1415750600, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(12, 5, 1415837000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(13, 6, 1416096200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(14, 6, 1416096200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(15, 6, 1416096200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(16, 6, 1416096200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(17, 6, 1416096200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(18, 6, 1416096200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(19, 7, 1416701000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(20, 7, 1416701000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(21, 7, 1416701000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(22, 7, 1416701000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(23, 7, 1416701000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(24, 8, 1417392200, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(25, 9, 1418342600, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(26, 9, 1418342600, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(27, 9, 1418342600, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(28, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(29, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(30, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(31, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(32, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(33, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(34, 10, 1418429000, 'http://somesite.com/interviewlink'); 
INSERT INTO `support_interviews` VALUES(35, 10, 1418429000, 'http://somesite.com/interviewlink'); 

回答

2

的功能。如果你想只当月访谈而今年,一个解决方案是在SQL语句中添加一个WHERE子句

SELECT * 
FROM table 
WHERE date_format(from_unixtime(interv_date),'%Y-%m')=date_format(now(), '%Y-%m') 
+0

返回0结果 – Manvaril 2015-03-30 18:28:04

+0

我编辑我的答案与你的列名和FROM_UNIXTIME instructi上。您插入的最古老的值是2014-12(1418429000值)。所以,如果你尝试这些值,你会得到一个空的结果 – grungero 2015-03-30 18:40:21

+0

将where子句添加到我原来的查询作品,谢谢 – Manvaril 2015-03-30 20:33:06

1

您正在使用now() FROM_UNIXTIME,然后应用year功能。这将返回null

mysql> select year(from_unixtime(now())); 
+----------------------------+ 
| year(from_unixtime(now())) | 
+----------------------------+ 
|      NULL | 
+----------------------------+ 
1 row in set (0.00 sec) 

如果你正在努力寻找对现在的年份和月份,然后就可以直接使用在now()

mysql> select year(now()); 
+-------------+ 
| year(now()) | 
+-------------+ 
|  2015 | 
+-------------+ 
1 row in set (0.00 sec) 


mysql> select month(now()); 
+--------------+ 
| month(now()) | 
+--------------+ 
|   3 | 
+--------------+ 
1 row in set (0.00 sec) 
相关问题