Mysql知识点(1)distinct

(一)distinct的使用语法:

select distinct expression[,expression...] from tables [where conditions];

(二)distinct的作用:

在mysql的实际运用场景中,经常会遇到某一字段或多个字段数据重复的情况,当我们需要进行去重处理时,就可以使用distinct。

在使用distinct时需注意一下几点:

  1. distinct只可以在select中使用。
  2. 若distinct后只存在1个字段,则仅对该字段进行去重;若distinct后存在多个字段,则对这多个字段的组合进行去重。
  3. distinct只能在所有字段最前面使用。

(三)distinct具体使用

存在以下学生成绩表student,主键key为(name,subject):

namesubjecthigh_score
张三语文85
李四数学99
李四物理88
王五语文85
赵六英语100
王五语文85

1.返回各科最高分

仅对subject进行去重,输出subject和对应hight_score

select distinct subject from student

查询结果:

subjecthigh_score
语文85
数学99
物理88
英语100

2.去除表中的重复信息,输出所有有效值

对name+subject+high_score进行去重

select distinct name , subject , high_score from student

查询结果:

namesubjecthigh_score
张三语文85
李四数学99
李四物理88
王五语文85
赵六英语100
  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值