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

結果をMySQLにグループ化し、リストに表示しますか?


このために、ORDERBY-

と一緒にGROUPBYを使用します
select yourColumnName,count(*) as anyAliasName from yourTableName group by yourColumnName order by yourColumnName;

テーブルを作成しましょう-

mysql> create table demo7
−> (
−> id int NOT NULL AUTO_INCREMENT,
−> first_name varchar(50)
−> ,
−> primary key(id)
−> );
Query OK, 0 rows affected (1.22 sec)

挿入コマンド-

を使用して、いくつかのレコードをテーブルに挿入します。
mysql> insert into demo7(first_name) values('John');
Query OK, 1 row affected (0.09 sec)
mysql> insert into demo7(first_name) values('David');
Query OK, 1 row affected (0.22 sec)
mysql> insert into demo7(first_name) values('John');
Query OK, 1 row affected (0.07 sec)
mysql> insert into demo7(first_name) values('Bob');
Query OK, 1 row affected (0.27 sec)
mysql> insert into demo7(first_name) values('David');
Query OK, 1 row affected (0.11 sec)
mysql> insert into demo7(first_name) values('David');
Query OK, 1 row affected (0.09 sec)
mysql> insert into demo7(first_name) values('John');
Query OK, 1 row affected (0.26 sec)
mysql> insert into demo7(first_name) values('John');
Query OK, 1 row affected (0.09 sec)

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

mysql> select *from demo7;

これにより、次の出力が生成されます-

+----+------------+
| id | first_name |
+----+------------+
|  1 | John       |
|  2 | David      |
|  3 | John       |
|  4 | Bob        |
|  5 | David      |
|  6 | David      |
|  7 | John       |
|  8 | John       |
+----+------------+
8 rows in set (0.00 sec)

以下は、MySQLで結果をグループ化し、リストに表示するためのクエリです-

mysql> select first_name,count(*) as frequency from demo7 group by first_name order by first_name;

これにより、次の出力が生成されます-

+------------+-----------+
| first_name | frequency |
+------------+-----------+
| Bob        |         1 |
| David      |         3 |
| John       |         4 |
+------------+-----------+
3 rows in set (0.00 sec)

  1. MySQLは、レコードのリストからレコードを検索して置換します

    最初にテーブルを作成しましょう- mysql> create table DemoTable    -> (    -> ListOfName text    -> ); Query OK, 0 rows affected (0.66 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable values('Carol,Sam,John,David,Bob,Mike,Robert,John,Chris,James,Ja

  2. MySQLでSUMとFORMATを組み合わせて、結果をフォーマットします

    テーブルを作成しましょう- mysql> create table DemoTable1950    (    Amount float    ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1950 values(45.60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable19