现有一个表A 为各大汽车品牌表
结构为:id title cliks
1 奔驰 1
2 宝马 2
3 奥迪 3
4 丰田 0
5 本田 2
另一表B 为汽车型号表id title bid picnum
1 E350 1 0
2 S500 1 1
3 C200 1 0
4 M350 1 2
5 760i 2 1
6 535i 2 3
7 A8-4 3 0bid对应的就是表A中的ID。picnum是图片数目
现在我需要查询把表B中图片数目大于0的记录所对应的汽车品牌集合。并且表A中的clicks也要大于0。按现在的数据内容。应得到表A中ID为1,2的记录。因为他们各有一个型号的车图片数目大于0。而ID为3的没有。所以我这样写语句:
select top (pagesize * page) * from A left join B on a.id = b.bid where a.clicks > 0 and b.picnum > 0这样提到4条记录。2条是重复的。
2条奔驰,2条宝马。而我希望的是奔驰宝马各一条记录。现在请教一下应该如何写SQL语句。谢谢。
结构为:id title cliks
1 奔驰 1
2 宝马 2
3 奥迪 3
4 丰田 0
5 本田 2
另一表B 为汽车型号表id title bid picnum
1 E350 1 0
2 S500 1 1
3 C200 1 0
4 M350 1 2
5 760i 2 1
6 535i 2 3
7 A8-4 3 0bid对应的就是表A中的ID。picnum是图片数目
现在我需要查询把表B中图片数目大于0的记录所对应的汽车品牌集合。并且表A中的clicks也要大于0。按现在的数据内容。应得到表A中ID为1,2的记录。因为他们各有一个型号的车图片数目大于0。而ID为3的没有。所以我这样写语句:
select top (pagesize * page) * from A left join B on a.id = b.bid where a.clicks > 0 and b.picnum > 0这样提到4条记录。2条是重复的。
2条奔驰,2条宝马。而我希望的是奔驰宝马各一条记录。现在请教一下应该如何写SQL语句。谢谢。
--上面是找到B表中picnum>0且bid相同时id最大的记录集
select top (pagesize * page) * from A left join
(select * from B where picnum>0 and not exists (select id from B tb1 where B.bid=tb1.bid and tb1.id>B.id)) tb
on a.id = tb.bid where a.clicks > 0
(SELECT * FROM 表A WHERE cliks > 0) a
CROSS APPLY
(SELECT TOP(1) * FROM 表B WHERE bid = a.id AND picnum > 0) b
(SELECT id=MIN(id), bid FROM 表B WHERE picnum > 0 GROUP BY bid) a
INNER JOIN 表A b
ON a.bid = b.id AND b.cliks > 0
INNER JOIN 表B c
ON a.id = c.id
--对B表左连接前做一个去重,或者查询出之后去重也行!select top (pagesize * page) *
from A left join
(select * from B t where picnum > 0 and not exists
(select id from B where bid = t.bid and id > t.id and picnum > 0)) e
on a.id = e.bid
where a.clicks > 0
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-04-29 08:46:10
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[title] varchar(4),[cliks] int)
insert [a]
select 1,'奔驰',1 union all
select 2,'宝马',2 union all
select 3,'奥迪',3 union all
select 4,'丰田',0 union all
select 5,'本田',2
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([id] int,[title] varchar(4),[bid] int,[picnum] int)
insert [b]
select 1,'E350',1,0 union all
select 2,'S500',1,1 union all
select 3,'C200',1,0 union all
select 4,'M350',1,2 union all
select 5,'760i',2,1 union all
select 6,'535i',2,3 union all
select 7,'A8-4',3,0
--------------开始查询--------------------------
select
*
from
a
join
b bb
on
a.id=bb.bid
and
a.cliks > 0 and bb.picnum > 0
and
bb.[picnum]=(select max([picnum]) from b where bb.bid=bid)----------------结果----------------------------
/* id title cliks id title bid picnum
----------- ----- ----------- ----------- ----- ----------- -----------
1 奔驰 1 4 M350 1 2
2 宝马 2 6 535i 2 3(2 行受影响)*/
(
SELECT * FROM 表B b
WHERE picnum > 0 AND NOT EXISTS(SELECT 1 FROM 表B WHERE bid = b.bid AND id < b.id AND picnum > 0)
) b
INNER JOIN 表A a
ON a.id = b.bid AND a.cliks > 0