1

问题是 1995 年和 1996 年有多少次采访。您必须使用 IN 关键字进行比较。还有一个名为 YEAR() 的函数将返回日期的年份部分。

桌子:

mysql> SELECT interviewdate "Date"
    -> FROM interview;    

+------------+
| Date       |
+------------+
| 1995-06-01 |
| 1995-06-01 |
| 1995-06-30 |
| 1995-06-30 |
| 1995-07-01 |
| 1995-08-01 |
| 1995-08-01 |
| 1995-08-02 |
| 1995-12-01 |
| 1995-12-02 |
| 1995-12-04 |
| 1996-01-21 |
| 1996-02-01 |
| 1996-02-02 |
| 1996-07-01 |
| 1996-07-01 |
| 1996-08-01 |
| 1996-08-08 |
| 1996-08-11 |
| 1997-01-01 |
| 1997-01-01 |
| 1997-01-31 |
| 1997-02-01 |
| 1997-03-24 |
| 1997-03-31 |
| 1997-04-20 |
| 1997-04-22 |
| 1997-05-01 |
+------------+
28 rows in set (0.00 sec)

输出:

条件是仅将日期为 1995 和 1996 的那些计数到新表 AS“Count(*)”中。有谁知道如何做到这一点?我是 mySQL 的新手,并试图理解语法。我试过这样的事情:

SELECT interviewdate, COUNT(interviewdate) AS "COUNT(*)"
FROM interview
GROUP BY interviewdate
HAVING COUNT(interviewdate) == 1995 AND 1996;


+----------+
| COUNT(*) |
+----------+
|       19 |
+----------+
1 row in set (0.00 sec)
4

2 回答 2

0

这应该有效:

select count(*)
from interview
where year(interviewdate) in (1995,1996)

如果您需要将结果插入到新表中,请使用create table as--more fiddle

于 2013-07-21T20:35:36.837 回答
0

尝试:

SELECT COUNT(*) AS number FROM interview WHERE (interviewdate > 1995-12-31 AND interviewdate < 1997-01-01)
于 2013-07-21T20:01:59.867 回答