数据表user_test如下,请你查询所有投递用户user_id并且进行去重展示,查询结果和返回顺序如下
查询结果和返回顺序如下所示
解题思路:
(1) 对user_id列直接去重:
(2)去重方式distinct或者是group by
(3)需要对结果进行排序 利用order by
drop table if exists user_test;
CREATE TABLE `user_test` (
`user_id` int NOT NULL,
`job_id` int NOT NULL,
`device_id` varchar(14) NOT NULL,
`job_salary` varchar(32) ,
`deliver_date` date NOT NULL);
INSERT INTO user_test VALUES(101,14550, 'app','838900', '2020-03-01');
INSERT INTO user_test VALUES(102,14550, 'pc','67356','2021-07-07');
INSERT INTO user_test VALUES(103,23654, 'pc',null, '2021-04-09');
INSERT INTO user_test VALUES(102,23152, 'app','297032','2022-03-17');
INSERT INTO user_test VALUES(105,75432, 'pc','156770', '2006-08-15');
select * from user_test;
select distinct user_id from user_test;
order by user_id
select user_id from user_test
group by user_id
order by user_id
MySQL中的DISTINCT和GROUP BY是两个常用的SQL语句,它们的作用和用法如下:
SELECT DISTINCT 列名1, 列名2, ...
FROM 表名
WHERE 条件;
例如,从一个名为students的表中查询所有不同的性别:
SELECT DISTINCT gender
FROM students;
SELECT
列名1,
列名2,
...,
aggregate_function(列名)
FROM 表名
WHERE 条件
GROUP BY 列名1, 列名2, ...;
例如,从一个名为orders
的表中按产品类别分组并计算每个类别的总销售额:
SELECT product_category, SUM(price * quantity) as total_sales
FROM orders
GROUP BY product_category;
公众号回复加油即可获取PDF版本