MySQLの特定のセルからのみ合計を取得しますか?
特定のセルについてのみ、WHEREで条件を設定し、集計関数SUM()を使用して追加します。まずテーブルを作成しましょう-
mysql> create table DemoTable1370 -> ( -> StudentName varchar(20), -> Marks int -> ); Query OK, 0 rows affected (0.87 sec)
挿入コマンド-
を使用して、テーブルにいくつかのレコードを挿入しますmysql> insert into DemoTable1370 values('Adam Smith',56);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1370 values('Chris Brown',67);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1370 values('Adam Smith',69);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1370 values('David Miller',98);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1370 values('John Smith',59);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1370 values('Adam Smith',79);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1370 values('Chris Brown',77);
Query OK, 1 row affected (0.17 sec) selectステートメントを使用してテーブルのすべてのレコードを表示する-
mysql> select * from DemoTable1370;
これにより、次の出力が生成されます-
+--------------+-------+ | StudentName | Marks | +--------------+-------+ | Adam Smith | 56 | | Chris Brown | 67 | | Adam Smith | 69 | | David Miller | 98 | | John Smith | 59 | | Adam Smith | 79 | | Chris Brown | 77 | +--------------+-------+ 7 rows in set (0.00 sec)
これが特定のセルから合計を取得するクエリです-
mysql> select sum(Marks) AS Total from DemoTable1370 where StudentName='Adam Smith';
これにより、次の出力が生成されます-
+-------+ | Total | +-------+ | 204 | +-------+ 1 row in set (0.00 sec)
-
特定のMySQL行から単一の値のみを取得しますか?
このためには、where句でSELECTINTO変数を使用します。まずテーブルを作成しましょう- mysql> create table DemoTable1896 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20), StudentMarks int ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、
-
行から特定の行を取得するためのMySQLクエリ
まずテーブルを作成しましょう- mysql> create table DemoTable1972 ( Section char(1), StudentName varchar(20) ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1972 values('D','Chris'); Quer