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

特定の月のすべてのエントリを選択するためのMySQLクエリ


MySQLの特定の月からすべてのエントリを選択するには、monthname()またはmonth()関数を使用します。

構文は次のとおりです。

select *from yourTableName where monthname(yourColumnName)='yourMonthName';

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

mysql> create table selectAllEntriesDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ShippingDate datetime
   -> );
Query OK, 0 rows affected (0.63 sec)

挿入コマンドを使用して、テーブルにいくつかのレコードを挿入します。

クエリは次のとおりです

mysql> insert into selectAllEntriesDemo(ShippingDate) values('2019-01-21');
Query OK, 1 row affected (0.24 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2018-02-24');
Query OK, 1 row affected (0.15 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2010-10-22');
Query OK, 1 row affected (0.20 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2011-04-12');
Query OK, 1 row affected (0.12 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2013-02-10');
Query OK, 1 row affected (0.18 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2014-02-15');
Query OK, 1 row affected (0.16 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2016-06-14');
Query OK, 1 row affected (0.18 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2017-02-14');
Query OK, 1 row affected (0.51 sec)
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2015-03-29');
Query OK, 1 row affected (0.19 sec)
>

selectステートメントを使用してテーブルのすべてのレコードを表示します。

クエリは次のとおりです。

mysql> select *from selectAllEntriesDemo;

以下は出力です。

+----+---------------------+
| Id | ShippingDate        |
+----+---------------------+
| 1  | 2019-01-21 00:00:00 |
| 2  | 2018-02-24 00:00:00 |
| 3  | 2010-10-22 00:00:00 |
| 4  | 2011-04-12 00:00:00 |
| 5  | 2013-02-10 00:00:00 |
| 6  | 2014-02-15 00:00:00 |
| 7  | 2016-06-14 00:00:00 |
| 8  | 2017-02-14 00:00:00 |
| 9  | 2015-03-29 00:00:00 |
+----+---------------------+
9 rows in set (0.00 sec)
>

以下は、特定の月のすべてのエントリを選択するためのクエリです。

mysql> select *from selectAllEntriesDemo where monthname(ShippingDate)='February';

これが出力です。

+----+---------------------+
| Id | ShippingDate        |
+----+---------------------+
| 2  | 2018-02-24 00:00:00 |
| 5  | 2013-02-10 00:00:00 |
| 6  | 2014-02-15 00:00:00 |
| 8  | 2017-02-14 00:00:00 |
+----+---------------------+
4 rows in set (0.00 sec)
>

これが代替クエリです。

mysql> select *from selectAllEntriesDemo where month(ShippingDate)=2;

以下は出力です。

+----+---------------------+
| Id | ShippingDate        |
+----+---------------------+
| 2  | 2018-02-24 00:00:00 |
| 5  | 2013-02-10 00:00:00 |
| 6  | 2014-02-15 00:00:00 |
| 8  | 2017-02-14 00:00:00 |
+----+---------------------+
4 rows in set (0.04 sec)
>
  1. 月に基づいてMySQLテーブルから合計を選択します

    このために、GROUP BY MONTH()を使用できます。まずテーブルを作成しましょう- mysql> create table DemoTable1628     -> (     -> PurchaseDate date,     -> Amount int     -> ); Query OK, 0 rows affected (1.55 sec) 挿入コマンドを使用して、テーブルにいくつかのレコードを挿入します。 mysql> insert into DemoTabl

  2. テーブルの個別の列から平均を選択するMySQLクエリ?

    平均を取得するには、AVG()を使用し、それをDISTINCTとともに使用して、個別のレコードから計算します。まずテーブルを作成しましょう- mysql> create table DemoTable1934    (    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into