您需要将OR条件与WHERE子句一起使用。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, MonthNumber int, YearNumber int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(MonthNumber,YearNumber) values(11,2018); mysql> insert into DemoTable(MonthNumber,YearNumber) values(3,2019); mysql> insert into DemoTable(MonthNumber,YearNumber) values(12,2018); mysql> insert into DemoTable(MonthNumber,YearNumber) values(1,2019); mysql> insert into DemoTable(MonthNumber,YearNumber) values(12,2019); mysql> insert into DemoTable(MonthNumber,YearNumber) values(6,2018);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------------+------------+ | Id | MonthNumber | YearNumber | +----+-------------+------------+ | 1 | 11 | 2018 | | 2 | 3 | 2019 | | 3 | 12 | 2018 | | 4 | 1 | 2019 | | 5 | 12 | 2019 | | 6 | 6 | 2018 | +----+-------------+------------+ 6 rows in set (0.00 sec)
以下是按月和年获取结果的查询-
mysql> select Id,MonthNumber,YearNumber from DemoTable where (MonthNumber >= 11 AND YearNumber = 2019) OR (MonthNumber <=11 AND YearNumber = 2018);
这将产生以下输出-
+----+-------------+------------+ | Id | MonthNumber | YearNumber | +----+-------------+------------+ | 1 | 11 | 2018 | | 5 | 12 | 2019 | | 6 | 6 | 2018 | +----+-------------+------------+ 3 rows in set (0.00 sec)