2015-03-30 164 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) 
相關問題