MySQL group by 单独 id 而不使用 GROUP BY 删除重复的列行?

mysqlmysqli database更新于 2024/10/9 11:56:17

为此,您可以使用 DISTINCT 关键字。让我们首先创建一个表 −

mysql> create table DemoTable1801
     (
     Name varchar(20),
     Score int
     );
Query OK, 0 rows affected (0.00 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable1801 values('John',98);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1801 values('John',98);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1801 values('John',99);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1801 values('Carol',99);
Query OK, 1 row affected (0.00 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1801;

这将产生以下输出 −

+-------+-------+
| Name  | Score |
+-------+-------+
| John  |    98 |
| John  |    98 |
| John  |    99 |
| Carol |    99 |
+-------+-------+
4 rows in set (0.00 sec)

这是按单独的 ID 进行分组的查询 −

mysql> select distinct Name,Score from DemoTable1801;

这将产生以下输出 −

+-------+-------+
| Name  | Score |
+-------+-------+
| John  |    98 |
| John  |    99 |
| Carol |    99 |
+-------+-------+
3 rows in set (0.00 sec)

相关文章


有用资源