- 场景:查询学生表(student)中总成绩(total_score)不等于500的人数。
select count(*) from student
where total_score <> 500
- 使用 != 500行不行? of course!
select count(*) from student
where total_score != 500
- 重点:为什么更建议使用 <> 来排除某值
因为SQL 的语法标准中规定了使用 <> 来表示不等于,而不是 !=。虽然有些数据库系统(如MySQL、PostgreSQL等)也支持使用 != 来表示不等于,但这并不是 SQL 的官方标准。
一句话: 为了保证不同类型数据库的兼容性