sql过滤重复记录

aigoo 2009-03-17 02:48:27
表1如下

<table width="200" border="1">
<tr>
<td>id</td>
<td>userid</td>
<td>bookid</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>5</td>
</tr>
<tr>
<td>4</td>
<td>3</td>
<td>5</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>5</td>
</tr>
</table>


表2如下:

<table width="200" border="1">
<tr>
<td>Url</td>
<td>bookid</td>
</tr>
<tr>
<td>defaultPic1.jpg</td>
<td>3</td>
</tr>
<tr>
<td>defaultPic2.jpg</td>
<td>4</td>
</tr>
<tr>
<td height="21">defaultPic3.jpg</td>
<td>5</td>
</tr>
<tr>
<td>defaultPic4.jpg</td>
<td>5</td>
</tr>
<tr>
<td>defaultPic5.jpg</td>
<td>5</td>
</tr>
</table>

最终得到的结果为

<table width="200" border="1">
<tr>
<td>url</td>
<td>userid</td>
<td>bookid</td>
</tr>
<tr>
<td>defaultPic1</td>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>defaultPic2</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>defaultPic3</td>
<td>3</td>
<td>5</td>
</tr>
</table>

请问怎么样写SQL才能得到最终的结果;
...全文
375 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxmtx___2008 2009-03-17
  • 打赏
  • 举报
回复
up
aigoo 2009-03-17
  • 打赏
  • 举报
回复

问题解决 谢谢大家,结貼
zzxap 2009-03-17
  • 打赏
  • 举报
回复
3 3 5
4 3 5
4 2 5

关键是这三条数据你想要那一条?
阿非 2009-03-17
  • 打赏
  • 举报
回复

declare @tableOne table([id] int,[userid] int,[bookid] int)
declare @tableTwo table([url] varchar(20),[bookid] int)

insert into @tableOne select 1,1,3
union all select 2,2,4
union all select 3,3,5
union all select 4,3,5
union all select 4,2,5

select * from @tableOne

insert into @tableTwo select 'default.Pic1.jpg',3
union all select 'default.Pic2.jpg',4
union all select 'default.Pic3.jpg',5
union all select 'default.Pic4.jpg',5
union all select 'default.Pic5.jpg',5

select * from @tableTwo

select two.[url],one.[userid],one.[bookid]
from (select * from @tableOne a where [id]=(select min([id]) from @tableOne where [userid]=a.[userid] )) one
inner join (select [bookid],min([url]) url from @tableTwo group by [bookid]) two
on one.[bookid]=two.[bookid]
che2piaopiao 2009-03-17
  • 打赏
  • 举报
回复
Distinct
zzxap 2009-03-17
  • 打赏
  • 举报
回复

[code=SQL]
改了
select b.url ,a.userid,a.bookid
from
(
select * from table1 c where exists (select top 1 bookid from table1 where bookid=c.bookid)
)a left join table2 b on a.bookid=b.bookid
[/CODE]
zzxap 2009-03-17
  • 打赏
  • 举报
回复

[code=SQL]
看我的
select b.url ,a.userid,a.bookid
from
(
select * from table1 a where exists (select top 1 bookid from table1 where bookid=a.bookid)
)b left join table2 b on a.bookid=a.bookid
[/CODE]
CutBug 2009-03-17
  • 打赏
  • 举报
回复
改一下
select d.url,c.userid,c.bookid from (
select userid,bookid from @tbl1 a
where not exists(
select * from @tbl1 where userid<>a.userid and id=a.id
)
) c
join (
select url,bookid from @tbl2 b
where not exists(
select * from @tbl2 where bookid=b.bookid and url<b.url
)
) d on d.bookid=c.bookid
/*返回:
url userid bookid
-------------------------------------------------- ----------- -----------
defaultPic1.jpg 1 3
defaultPic2.jpg 2 4
defaultPic3.jpg 3 5
*/
CutBug 2009-03-17
  • 打赏
  • 举报
回复
declare @tbl1 table(id int ,userid int ,bookid int )
insert into @tbl1
select 1, 1, 3 union all
select 2, 2, 4 union all
select 3, 3, 5 union all
select 4, 3, 5 union all
select 4, 2, 5
declare @tbl2 table(url varchar(50),bookid int )
insert into @tbl2
select 'defaultPic1.jpg', 3 union all
select 'defaultPic2.jpg', 4 union all
select 'defaultPic3.jpg', 5 union all
select 'defaultPic4.jpg', 5 union all
select 'defaultPic5.jpg', 5

select d.url,c.userid,c.bookid from (
select userid,bookid from @tbl1 a
where not exists(
select * from @tbl1 where userid<>a.userid and id=a.id
)
) c
join (
select url,bookid from @tbl2 b
where not exists(
select * from @tbl2 where bookid=b.bookid and url>b.url
)
) d on d.bookid=c.bookid

/*输出结果
url userid bookid
-------------------------------------------------- ----------- -----------
defaultPic1.jpg 1 3
defaultPic2.jpg 2 4
defaultPic5.jpg 3 5
*/
aigoo 2009-03-17
  • 打赏
  • 举报
回复
表1如下
id userid bookid
1 1 3
2 2 4
3 3 5
4 3 5
4 2 5
表2如下:
Url bookid
defaultPic1.jpg 3
defaultPic2.jpg 4
defaultPic3.jpg 5
defaultPic4.jpg 5
defaultPic5.jpg 5
最终得到的结果为
url userid bookid
defaultPic1 1 3
defaultPic2 2 4
defaultPic3 3 5
请问怎么样写SQL才能得到最终的结果;
lisir474465915 2009-03-17
  • 打赏
  • 举报
回复
期待答案

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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