超级SQL疑难问题 高手请进

7仔 2007-02-10 03:12:04
我有一个表 结构如下
表 A
产品类型
大米
白面
豆油
表 B
用户名 产品类型 数量 id(自动增长) 时间(自动记录)
1 大米 10 1
1 大米 9 2
1 白面 5 3
2 白面 3 4
2 大米 5 5
我要根据表A 查询表B 中 用户名为1的 所有的产品类型、数量、时间是最新的
我要得到的结果是
用户1 产品类型 大米 9 白面 5 得到这个结果
...全文
274 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
marco08 2007-02-10
  • 打赏
  • 举报
回复

select * from B as tmp
where not exists(select 1 from B on 用户名=tmp.用户名 and 产品类型=tmp.产品类型 and 时间>tmp.时间)
茂一茂 2007-02-10
  • 打赏
  • 举报
回复
Windows2003Server 繁體版

VisualBasic6.0 SP6

不能輸入中文,輸入變成亂碼?

現在隻能從Word裡打完字後再拷到VB中去(用Notepad還不行),麻煩

有沒有辦法解決呢?
chuifengde 2007-02-10
  • 打赏
  • 举报
回复
declare @a table(产品类型 varchar(10))
insert @a select '大米'
union all select '白面'
union all select '豆油'
declare @b table(用户名 int, 产品类型 varchar(10), 数量 int, id int, 时间 smalldatetime)
insert @b select 1 ,'大米', 10, 1 ,null
union all select 1 ,'大米', 9, 2,null
union all select 1 ,'白面', 5, 3,null
union all select 2 ,'白面', 3, 4,null
union all select 2 ,'大米', 5, 5,null

select * from @b b where 用户名=1 and not exists(select 1 from @b where 产品类型=b.产品类型 and id>b.id and 用户名=1)
zheninchangjiang 2007-02-10
  • 打赏
  • 举报
回复
select * from tableb aa where id=(select max(id) from tableb where 用户名=aa.用户名)
and 用户名=1

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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