首页 > 试题广场 >

查询结果去重

[编程题]查询结果去重
  • 热度指数:519856 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
题目:现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。

示例:user_profile
id device_id gender age university province
1 2138 male 21 北京大学 Beijing
2 3214 male
复旦大学 Shanghai
3 6543 female 20 北京大学 Beijing
4 2315 female 23 浙江大学 ZheJiang
5 5432 male 25 山东大学 Shandong
根据示例,你的查询应返回以下结果:
university
北京大学
复旦大学
浙江大学
山东大学
示例1

输入

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

输出

北京大学
复旦大学
浙江大学
山东大学
查询结果去重。
SELECT DISTINCT university
FROM user_profile
发表于 2026-02-19 13:10:24 回复(0)
SELECT DISTINCT 字段列表 FROM 表名;
DISTINCT 简单去重,不管数量多少
GROUP BY 分组统计,各种计算都好
去重用前者,统计用后者
发表于 2026-01-16 18:04:16 回复(0)
select
    distinct(university)
from user_profile
发表于 2025-10-29 20:27:57 回复(0)
为什么总显示答案错误?
发表于 2025-02-04 21:18:41 回复(0)
SELECT DISTINCT
    university
FROM
    user_profile;

发表于 2025-01-01 20:53:51 回复(0)
select distinct 
university
from user_profile;

select
university
from user_profile
group by university;

# 1表示根据第一个字段
select
university
from user_profile
group by 1;

发表于 2024-08-29 23:15:07 回复(0)
去重代码放在字段前面
SELECt  distinct university FROM user_profile
发表于 2024-08-15 16:43:34 回复(0)
为什么聚合函数group by 比distinct运行时间快,占用空间小

发表于 2024-06-05 12:00:29 回复(0)
DISTINCT 用于从查询结果中去除重复的行。它可以应用于单个列或多个***保返回的结果中每行都是唯一的。例如,如果你有一个包含多个重复行的表,使用 DISTINCT 可以得到不重复的行。

GROUP BY 则用于根据一个或多个列对数据进行分组。它会将具有相同值的行分组在一起,并对每个组进行聚合计算(如求和、计数等)。通常与聚合函数(如 SUM()、COUNT())一起使用,以对每个组应用特定的计算。
编辑于 2024-03-16 15:04:44 回复(0)
查重有两种写法:
  1. 第一种就是查询所需要的数据信息对其进行分组;
  2. 第二种就是将查询出来的信息进行去重操作 distinct
编辑于 2024-03-05 15:33:11 回复(0)
select
university
from user_profile
group by 1
发表于 2023-04-24 11:03:05 回复(0)
使用distinct
-- select distinct university from user_profile ;
使用group by
select university from user_profile group by university;

发表于 2023-03-24 16:27:40 回复(0)
SELECT DISTINCT university  from user_profile
发表于 2023-03-08 18:47:04 回复(0)
方法一:使用distinct关键字。
select distinct university from user_profile;
方法二:使用union查询。
select university from user_profile
union  
select university from user_profile;
方法三:使用group by.
select university from user_profile group by university;
发表于 2023-03-01 18:34:28 回复(0)
我去,全网最笨解法
Select university
From user_profile
where age >20 or age is null

发表于 2023-02-23 05:37:10 回复(1)
为什么你们都没有说到 union 这个:
select university from user_profile
union
select university from user_profile
发表于 2023-01-31 11:57:02 回复(6)

【分类】:查询结果去重

分析思路

select 查询结果 [distinct 学校]
from 从哪张表中查找数据 [user_profile]

求解代码

select
    distinct university 
from user_profile
发表于 2022-11-24 19:20:05 回复(0)
select distinct  university from user_profile

发表于 2022-10-10 16:19:32 回复(0)
select distinct university from user_profile;
发表于 2022-09-20 17:04:49 回复(0)