如何在 MySQL 中忽略特定记录并添加其余相应的记录(数字)?
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> Name varchar(20), -> Amount int -> ); Query OK, 0 rows affected (0.61 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John',200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris',150); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Mike',500); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('John',350); Query OK, 1 row affected (0.18 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable;
这将产生以下输出 −
+-------+--------+ | Name | Amount | +-------+--------+ | John | 200 | | Chris | 150 | | Mike | 500 | | John | 350 | +-------+--------+ 4 rows in set (0.00 sec)
这是忽略特定记录并添加剩余记录(数字)的查询 −
mysql> select sum(Amount) from DemoTable -> where Name!='John';
这将产生以下输出 −
+-------------+ | sum(Amount) | +-------------+ | 650 | +-------------+ 1 row in set (0.00 sec)