Access高手,查询关于重复记录问题??双表

reyoreyoreyo 2009-10-16 05:26:54
引用 13 楼 acmain_chm 的回复:
SQL codeselecttop3*from t_reyoreyoreyo twhere nclassid='Bush'andnotexists (select1from t_reyoreyoreyowhere nclassid='Bush'and O_Id <t.O_Idand (opertiontime=t.opertiontimeor unitprice=t.unitprice
¡­




select top 6 * from "+enttable+" t where nclassid=? and not exists ( select 1 from "+enttable+" where nclassid=? and goodsid <t.goodsid and (opertiontime=t.opertiontime or unitprice=t.unitprice) ) order by opertiontime desc


我还想显示 "+usitable+" 中的 company 字段,"+usitable+"与 "+enttable+" 通过
"+usitable+".username="+enttable+".buyform

我这样写不行
select top 6 * from "+enttable+" t where nclassid=? and not exists ( select 1 from "+enttable+","+usitable+" where "+usitable+".username="+enttable+".buyform and "+usitable+".nclassid=? and goodsid <t.goodsid and (opertiontime=t.opertiontime or unitprice=t.unitprice) ) order by opertiontime desc

望高手帮忙啊
...全文
441 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
reyoreyoreyo 2009-10-22
  • 打赏
  • 举报
回复
自己搞定了,谢谢 ACMAIN_CHM 热心帮忙
reyoreyoreyo 2009-10-19
  • 打赏
  • 举报
回复
reyo_enter 表中
1。 如何排序? 也就是你所谓的 前5条记录 是否以 价格排序?
答:按时间降序排列

2。 不同时间(opertiontime)不同价格(unitprice) 是指什么 ?
答:就是价格与时间不相同的记录(同时满足)

reyo_enter
goodsid buyform nclassid unitprice opertiontime
2 aa Bush 1000 8/30/2009 4:47:27 PM 取上
3 bb Bush 1000 8/30/2009 4:41:27 PM 没取上
4 cc Bush 8000 8/30/2009 4:41:27 PM 取上
5 dd Bush 700 8/30/2009 4:47:27 PM 取上
6 ee Bush 800 8/29/2009 4:41:27 PM 取上
7 ff Bush 100 8/28/2009 4:47:27 PM 取上
8 gg Bush 100 8/25/2009 1:41:27 PM 没取上
ACMAIN_CHM 2009-10-18
  • 打赏
  • 举报
回复
reyo_enter
goodsid buyform nclassid unitprice opertiontime
2 aa Bush 1000 8/30/2009 4:47:27 PM
3 bb Bush 1000 8/30/2009 4:41:27 PM
4 cc Bush 8000 8/30/2009 4:41:27 PM 这条为什么取上?
5 dd Bush 700 8/30/2009 4:47:27 PM 这条为什么没取上?
6 ee Bush 800 8/29/2009 4:41:27 PM
7 ff Bush 100 8/28/2009 4:47:27 PM
8 gg Bush 100 8/25/2009 1:41:27 PM

针对上面两条能解释一下吗?

现在把你的问题先简化一下。

reyo_enter 表中
1。 如何排序? 也就是你所谓的 前5条记录 是否以 价格排序?
2。 不同时间(opertiontime)不同价格(unitprice) 是指什么 ?
reyoreyoreyo 2009-10-18
  • 打赏
  • 举报
回复
不好意思 是笔误,感谢你的热心帮助

我想实现:查询指定表的("+enttable+" )的指定分类(nclassid=?)的,不同时间(opertiontime)不同价格(unitprice)的前5条记录(按价格排序)。

如我查询 nclassid=Bush 时,结果应如下:

opertiontime unitprice nclassid company
2009-08-30 16:47:27 1000 Bush 公司一
2009-08-30 16:41:27 1000 Bush 公司二
2009-08-30 16:41:27 8000 Bush 公司三
2009-08-30 16:47:27 800 Bush 公司四
2009-08-28 16:47:27 100 Bush 公司六
ACMAIN_CHM 2009-10-18
  • 打赏
  • 举报
回复
楼主,请认真点吧。

下面是你自己提供的测试用HC.MDB中的数据。请根据这些数据给出你的期望的结果!!! ( 1 2009-08-30 16:47:27 1000 Bush 公司一 这个记录中的 O_id =1 是哪儿来的?????) 虽然在论坛上你获得的帮助是免费的,但并不代表不负责的提问不浪费别人的时间和资源!!!

reyo_enter
goodsid buyform nclassid unitprice opertiontime
2 aa Bush 1000 8/30/2009 4:47:27 PM
3 bb Bush 1000 8/30/2009 4:41:27 PM
4 cc Bush 8000 8/30/2009 4:41:27 PM
5 dd Bush 700 8/30/2009 4:47:27 PM
6 ee Bush 800 8/29/2009 4:41:27 PM
7 ff Bush 100 8/28/2009 4:47:27 PM
8 gg Bush 100 8/25/2009 1:41:27 PM

reyo_userinfo
userid username company regtime
9 aa 公司一 4/6/2007 10:46:26 AM
11 bb 公司二 4/6/2007 10:46:26 AM
13 cc 公司三 4/6/2007 10:46:26 AM
15 dd 公司四 4/6/2007 10:46:26 AM
16 ee 公司五 4/6/2007 10:46:26 AM
17 ff 公司六 4/6/2007 10:46:26 AM
18 gg 公司七 4/6/2007 10:46:26 AM
reyoreyoreyo 2009-10-17
  • 打赏
  • 举报
回复
如果我的"+enttable+" 的记录特殊一点

O_Id opertiontime unitprice nclassid buyform
1 2009-08-30 11:47:27 1000 Bush aa
2 2009-08-30 12:41:27 1000 Bush bb
3 2009-08-30 14:42:27 700 Bush cc
4 2009-08-30 16:47:27 700 Bush dd
5 2009-08-30 18:47:27 100 Bush ee
6 2009-08-30 22:47:27 100 Bush ff

用你的SQL会得到正确的结果吗?查询指定表的("+enttable+" )的指定分类(nclassid=?)的,不同时间(opertiontime)不同价格(unitprice)的前3条记录
reyoreyoreyo 2009-10-17
  • 打赏
  • 举报
回复
O_Id opertiontime unitprice nclassid company
1 2009-08-30 16:47:27 1000 Bush 公司一
2 2009-08-29 16:47:27 100 Bush 公司五
3 2009-08-28 16:47:27 700 Bush 公司四

用你的方法得到以上结果
ACMAIN_CHM 2009-10-17
  • 打赏
  • 举报
回复
看到了你的两个表,可是没找到基于这些记录所对应的正确结果。

你能否手工准备一下基于这些数据的正确结果。这样别人给你方案前也可以比较一下是否达到你的要求。 否则这样反复的猜来猜去效率很差。

[Quote=1楼 ACMAIN_CHM(acmain) ]建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。[/Quote]
reyoreyoreyo 2009-10-17
  • 打赏
  • 举报
回复
可能是我的表达问题,不好意思

我之所以要用 reyo_userinfo 是想得到中文公司的名称

goodsid opertiontime unitprice nclassid company
2 8/30/2009 4:47:27 PM 1000 Bush aa
3 8/30/2009 4:41:27 PM 1000 Bush bb
4 8/30/2009 4:41:27 PM 8000 Bush cc
5 8/30/2009 4:47:27 PM 700 Bush dd
6 8/29/2009 4:41:27 PM 800 Bush ee

我想实现:查询指定表的("+enttable+" )的指定分类(nclassid=?)的,不同时间(opertiontime)不同价格(unitprice)的前5条记录(按价格排序)。

如我查询 nclassid=Bush 时,结果应如下:

O_Id opertiontime unitprice nclassid company
1 2009-08-30 16:47:27 1000 Bush 公司一
2 2009-08-30 16:41:27 1000 Bush 公司二
3 2009-08-30 16:41:27 8000 Bush 公司三
4 2009-08-30 16:47:27 800 Bush 公司四
5 2009-08-28 16:47:27 100 Bush 公司六
reyoreyoreyo 2009-10-17
  • 打赏
  • 举报
回复
已上传,谢谢了
ACMAIN_CHM 2009-10-17
  • 打赏
  • 举报
回复
能否提供你的测试用数据库MDB文件?


可以上传到 http://www.access911.net/csdn
ACMAIN_CHM 2009-10-17
  • 打赏
  • 举报
回复

楼主好象总是在考验人的猜测能力!


select *
from (
select top 5 a.goodsid,a.opertiontime,a.unitprice,a.nclassid,b.username as company
from reyo_enter a inner join reyo_userinfo b on a.buyform=b.username
order by a.unitprice desc
)
order by goodsid


goodsid opertiontime unitprice nclassid company
2 8/30/2009 4:47:27 PM 1000 Bush aa
3 8/30/2009 4:41:27 PM 1000 Bush bb
4 8/30/2009 4:41:27 PM 8000 Bush cc
5 8/30/2009 4:47:27 PM 700 Bush dd
6 8/29/2009 4:41:27 PM 800 Bush ee



你提供的测试用MDB文件和你贴子中的描述完全不一样!!! 也得不出你要的结果!!!

请节约一下别人的时间。
reyoreyoreyo 2009-10-17
  • 打赏
  • 举报
回复
"+enttable+" 的记录

O_Id opertiontime unitprice nclassid buyform
1 2009-08-30 16:47:27 1000 Bush aa
2 2009-08-30 16:41:27 1000 Bush bb
3 2009-08-30 16:41:27 8000 Bush cc
4 2009-08-30 16:47:27 800 Bush dd
5 2009-08-29 16:41:27 800 Bush ee
6 2009-08-28 16:47:27 100 Bush ff
7 2009-08-25 13:41:27 100 Bush gg

"+usitable+"的记录

user_Id username company
1 aa 公司一
2 bb 公司二
3 cc 公司三
4 dd 公司四
5 ee 公司五
6 ff 公司六
7 gg 公司七

我想实现:查询指定表的("+enttable+" )的指定分类(nclassid=?)的,不同时间(opertiontime)不同价格(unitprice)的前5条记录(按价格排序)。

如我查询 nclassid=Bush 时,结果应如下:

O_Id opertiontime unitprice nclassid company
1 2009-08-30 16:47:27 1000 Bush 公司一
2 2009-08-30 16:41:27 1000 Bush 公司二
3 2009-08-30 16:41:27 8000 Bush 公司三
4 2009-08-30 16:47:27 800 Bush 公司四
5 2009-08-28 16:47:27 100 Bush 公司六

测试的数据库也已手工作好且上传了
reyoreyoreyo 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acmain_chm 的回复:]
SQL codeselecttop3 a.O_Id,a.opertiontime,a.unitprice,a.nclassid,b.companyfrom enttable a,usitable bwhere a.buyform=b.usernameorderby unitpricedesc
[/Quote]


能得到以下结果????????????????注意看要 不同时间(opertiontime)不同价格(unitprice)的前3条记录

我想实现:查询指定表的("+enttable+" )的指定分类(nclassid=?)的,不同时间(opertiontime)不同价格(unitprice)的前3条记录。


"+enttable+" 的记录

O_Id opertiontime unitprice nclassid buyform
1 2009-08-30 16:47:27 1000 Bush aa
2 2009-08-29 16:41:27 1000 Bush bb
3 2009-08-28 16:42:27 700 Bush cc
4 2009-08-28 16:47:27 700 Bush dd
5 2009-08-29 16:47:27 100 Bush ee
6 2009-08-28 13:47:27 100 Bush ff

"+usitable+"的记录

user_Id username company
1 aa 公司一
2 bb 公司二
3 cc 公司三
4 dd 公司四
5 ee 公司五
6 ff 公司六

如我查询 nclassid=Bush 时,结果应如下:
O_Id opertiontime unitprice nclassid company
1 2009-08-30 16:47:27 1000 Bush 公司一
2 2009-08-28 16:47:27 700 Bush 公司四
3 2009-08-29 16:47:27 100 Bush 公司五

ACMAIN_CHM 2009-10-16
  • 打赏
  • 举报
回复
select top 3 a.O_Id,a.opertiontime,a.unitprice,a.nclassid,b.company
from enttable a,usitable b
where a.buyform=b.username
order by unitprice desc
reyoreyoreyo 2009-10-16
  • 打赏
  • 举报
回复
我想实现:查询指定表的("+enttable+" )的指定分类(nclassid=?)的,不同时间(opertiontime)不同价格(unitprice)的前3条记录。


"+enttable+" 的记录

O_Id opertiontime unitprice nclassid buyform
1 2009-08-30 16:47:27 1000 Bush aa
2 2009-08-29 16:41:27 1000 Bush bb
3 2009-08-28 16:42:27 700 Bush cc
4 2009-08-28 16:47:27 700 Bush dd
5 2009-08-29 16:47:27 100 Bush ee
6 2009-08-28 13:47:27 100 Bush ff

"+usitable+"的记录

user_Id username company
1 aa 公司一
2 bb 公司二
3 cc 公司三
4 dd 公司四
5 ee 公司五
6 ff 公司六

如我查询 nclassid=Bush 时,结果应如下:
O_Id opertiontime unitprice nclassid company
1 2009-08-30 16:47:27 1000 Bush 公司一
2 2009-08-28 16:47:27 700 Bush 公司四
3 2009-08-29 16:47:27 100 Bush 公司五
ACMAIN_CHM 2009-10-16
  • 打赏
  • 举报
回复
没看懂你的需求。

建议你用些例子说明。

建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。

7,713

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点。
社区管理员
  • Access
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧