IF OBJECT_ID(N'a') IS NOT NULL DROP TABLE a
CREATE TABLE a
(id INT NOT NULL,
NAME VARCHAR(20),
class VARCHAR(20),
COUNT INT,
date DATETIME
)
INSERT INTO a VALUES ('1','苹果','水果',10,'2011-7-1'),('1','桔子','水果',20,'2011-7-2'),('1','香蕉','水果',15,'2011-7-3')
INSERT INTO a VALUES('2','白菜','蔬菜',12,'2011-7-1'), ('2','青菜','蔬菜',19,'2011-7-2')
如果想要得到下面的结果:(Id唯一,Date选最近的一次)
1 香蕉 水果 15 2011-7-3
2 青菜 蔬菜 19 2011-7-2
语句如下:
SELECT * FROM A T
WHERE NOT EXISTS
(SELECT * FROM A WHERE id=t.id AND date>t.date)
注:NOT EXISTS 的作用与 EXISTS 正相反。如果子查询没有返回行,则满足 NOT EXISTS 中的 WHERE 子句。
即Exists做为WHERE条件时,是先对where前的主查询进行查询,然后用主查询的结果一个一个的代入exists的子查询进行判断,如果为真则输出当前这一条主查询的结果,否则不输出
详细解决方案
Not Exists练习
热度:79 发布时间:2024-01-22 03:17:43.0
相关解决方案
- 有关File.Exists(filename)的有关问题
- error C2243: 'type cast' : conversion from 'class CTeacher *' to 'class CBase *' exists, but is inaccessible解决方案
- exists 与 where的执行过程,该怎么处理
- 重新安装后出现Server Name Already Exists,该如何处理
- images中明明有图片为何在file.exists()时,布尔值为false
- 两个表关联的条件不是一对一的关系是一对多或多对多的关系可以用exists和not exists,该怎么处理
- 请问一下一般exists 和not exists 一般在什么情况下用 能举个例子吗
- File f = new File(filePath) f.exists() 替false 求大神
- struts-config下创建form出现异常,"xxx already exists"
- drop an exists trigger but popup ora-04098,该怎么解决
- oracle exists 的用法,求解,该如何处理
- 用not exists 取最大值为什么没用,该如何解决
- exists 的有关问题
- Oracle 的 NOT EXISTS 很慢的有关问题
- 今天说一下子where 中 exists 和 in 里面的一些区别
- create table if not exists 20150519 (id int);该怎么处理
- create table person if not exists person mysql语句错哪了
- 怎么在 delete 中优化 not exists
- where not exists 与 if not exists 的差异
- File file = new File(D:\test\a.txt) 文件存在 但是file.exists()为false解决方案
- sql exists 没起作用, 哪里错
- t-sql if exists begin end 报错,该怎么处理
- 这条 not exists 表达式怎么写
- 把not in改成not exists
- 容易 NOT EXISTS
- NOT EXISTS,该怎么解决
- 当没有用 EXISTS 引来子查询时,在选择列表中只能指定一个表达式
- IF EXISTS 用法 来个老鸟帮帮忙
- not exists 求解。很迷惑,不知道他是如何执行的。多谢
- 如果用 exists 代替 in 那 not in 是否也有代替呢?解决思路