MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

MySQLは30日の範囲の日付を選択しますか?


30日の範囲の日付を選択するには、間隔を使用して算術演算を使用できます。

構文は次のとおりです-

select *from yourTableName
where yourDateColumnName > NOW() - INTERVAL 30 DAY
and yourDateColumnName < NOW() + INTERVAL 30 DAY;

上記の構文を理解するために、テーブルを作成しましょう。テーブルを作成するためのクエリは次のとおりです-

mysql> create table selectDatesDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ArrivalDate datetime
   -> );
Query OK, 0 rows affected (0.77 sec)

これで、insertコマンドを使用してテーブルにいくつかのレコードを挿入できます。クエリは次のとおりです-

mysql> insert into selectDatesDemo(ArrivalDate) values('2019-01-10');
Query OK, 1 row affected (0.12 sec)
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-01-29');
Query OK, 1 row affected (0.21 sec)
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-02-13');
Query OK, 1 row affected (0.14 sec)
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-02-19');
Query OK, 1 row affected (0.14 sec)
mysql> insert into selectDatesDemo(ArrivalDate) values('2018-02-13');
Query OK, 1 row affected (0.17 sec)
mysql> insert into selectDatesDemo(ArrivalDate) values('2018-03-13');
Query OK, 1 row affected (0.19 sec)

selectステートメントを使用して、テーブルのすべてのレコードを表示します。クエリは次のとおりです-

mysql> select *from selectDatesDemo;

これが出力です-

+----+---------------------+
| Id | ArrivalDate         |
+----+---------------------+
| 1  | 2019-01-10 00:00:00 |
| 2  | 2019-01-29 00:00:00 |
| 3  | 2019-02-13 00:00:00 |
| 4  | 2019-02-19 00:00:00 |
| 5  | 2018-02-13 00:00:00 |
| 6  | 2018-03-13 00:00:00 |
+----+---------------------+
6 rows in set (0.00 sec)
>

30日の範囲の日付を選択するためのクエリは次のとおりです-

mysql> select *from selectDatesDemo
   -> where ArrivalDate > NOW() - INTERVAL 30 DAY
   -> and ArrivalDate < NOW() + INTERVAL 30 DAY;

以下は出力です-

+----+---------------------+
| Id | ArrivalDate         |
+----+---------------------+
| 3  | 2019-02-13 00:00:00 |
| 4  | 2019-02-19 00:00:00 |
+----+---------------------+
2 rows in set (0.04 sec)
>
  1. MySQLのWHEREで日付を減算して行を選択しますか?

    これには、TIMESTAMPDIFF()を使用します。テーブルを作成しましょう- mysql> create table demo42 −> ( −> start_date datetime −> ); Query OK, 0 rows affected (0.77 sec) 挿入コマンド-を使用して、いくつかのレコードをテーブルに挿入します。 mysql> insert into demo42 values('2020-01-10 12:30:05'); Query OK, 1 row affected (0

  2. MySQLで条件付きで日付の間/前/後を選択するにはどうすればよいですか?

    以下は構文です- select *from yourTableName where yourColumnName1 < yourValue1 AND (yourColumnName2 > yourValue2 OR yourColumnName2 is null); テーブルを作成しましょう- mysql> create table demo35 −> ( −> id int NOT NULL AUTO_INCREMENT PRIMARY KEY, −> joining_date date, −> re