34,576
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: #userRole
if object_id('tempdb.dbo.#userRole') is not null drop table #userRole
create table #userRole (ID int,会员类型 nvarchar(8),会员排序 int)
insert into #userRole
select 1,'普通会员',0 union all
select 2,'高级会员',1
--> 测试数据: #userTable
if object_id('tempdb.dbo.#userTable') is not null drop table #userTable
create table #userTable (ID int,会员帐号 nvarchar(7),会员角色类型 int)
insert into #userTable
select 1,'user001',2 union all
select 2,'user002',2 union all
select 3,'user005',1 union all
select 4,'user008',2 union all
select 5,'user111',1 union all
select 6,'user114',1 union all
select 7,'user07',1 union all
select 8,'user08',1 union all
select 9,'user09',1 union all
select 11,'user101',1 union all
select 16,'user104',1 union all
select 17,'user17',2 union all
select 18,'user18',1 union all
select 19,'user19',2
--> 测试数据: #companyTable
if object_id('tempdb.dbo.#companyTable') is not null drop table #companyTable
create table #companyTable (ID int,公司名称 nvarchar(9),useID varchar(7),地址 varchar(10))
insert into #companyTable
select 1,'公司一','user001',null union all
select 2,'公司二','user002',null union all
select 3,'公司三','user005',null union all
select 4,'公司四','user111',null union all
select 5,'公司五','user114',null union all
select 6,'公司六','user008',null union all
select 7,'公司pp','user07',null union all
select 8,'公司yy','user08',null union all
select 10,'公司cc','user111',null union all
select 11,'公司tt','user114',null union all
select 12,'公司aa','user101',null union all
select 13,'公司er','user09',null union all
select 23,'公司wer','user104',null union all
select 26,'公司we','user17',null union all
select 27,'公司sdfsd','user19',null
--> 测试数据: #ProdTable
if object_id('tempdb.dbo.#ProdTable') is not null drop table #ProdTable
create table #ProdTable (ID int,公司ID int,产品名称 nvarchar(5))
insert into #ProdTable
select 1,1,'A产品' union all
select 2,1,'B产品' union all
select 3,1,'C产品' union all
select 4,2,'A产品' union all
select 5,3,'A产品' union all
select 6,23,'A产品' union all
select 7,6,'A产品' union all
select 8,11,'A产品' union all
select 9.,27,'A产品' union all
select 10,26,'A产品' union all
select 11,10,'A产品'
--> 测试数据: #CompanyAdsTable
if object_id('tempdb.dbo.#CompanyAdsTable') is not null drop table #CompanyAdsTable
create table #CompanyAdsTable (ID int,关键字名词 nvarchar(5),公司ID int,排序显示位置 int)
insert into #CompanyAdsTable
select 1,'A产品',3,1 union all
select 2,'E产品',3,1 union all
select 3,'B产品',5,1 union all
select 4,'A产品',6,2
go
select distinct e.排序显示位置*(-1) as 产品付费数,a.id as 是否会员, d.产品名称,c.公司名称,c.地址 from #userRole a
inner join #userTable b on a.id=b.会员角色类型
inner join #companyTable c on b.会员帐号=c.useid
inner join #prodTable d on c.id=d.公司id
left join #companyAdsTable e on d.公司id=e.公司id
where d.产品名称='A产品'
order by e.排序显示位置*(-1) desc, a.id desc
/*
产品付费数 是否会员 产品名称 公司名称 地址
----------- ----------- ----- --------- ----------
-1 1 A产品 公司三 NULL
-2 2 A产品 公司六 NULL
NULL 2 A产品 公司sdfsd NULL
NULL 2 A产品 公司we NULL
NULL 2 A产品 公司二 NULL
NULL 2 A产品 公司一 NULL
NULL 1 A产品 公司cc NULL
NULL 1 A产品 公司tt NULL
NULL 1 A产品 公司wer NULL
(9 行受影响)
*/
declare @s varchar(1000)
set @s = 'A产品' --需要查找的产品
select b.*,@s from
(select distinct 公司ID from #ProdTable where 产品名称=@s) a
left join #companyTable b on b.id = a.公司ID
order by
isnull((select rtrim(min(排序显示位置)) from #CompanyAdsTable where 关键字名词 = @s and 公司ID = a.公司ID),'a'),
(select max(会员排序) from #userRole c,#userTable d where c.id=d.会员角色类型 and d.会员帐号 = b.useID) desc
set nocount on
--> 测试数据: #userRole
if object_id('tempdb.dbo.#userRole') is not null drop table #userRole
create table #userRole (ID int,会员类型 nvarchar(8),会员排序 int)
insert into #userRole
select 1,'普通会员',0 union all
select 2,'高级会员',1
--> 测试数据: #userTable
if object_id('tempdb.dbo.#userTable') is not null drop table #userTable
create table #userTable (ID int,会员帐号 nvarchar(7),会员角色类型 int)
insert into #userTable
select 1,'user001',1 union all
select 2,'user002',2 union all
select 3,'user005',2 union all
select 4,'user008',1 union all
select 5,'user111',1 union all
select 6,'user114',1
--> 测试数据: #companyTable
if object_id('tempdb.dbo.#companyTable') is not null drop table #companyTable
create table #companyTable (ID int,公司名称 nvarchar(6),useID varchar(7),地址 nvarchar(2))
insert into #companyTable
select 1,'公司一','user001','aa' union all
select 2,'公司二','user002','aa' union all
select 3,'公司三','user005','bb' union all
select 4,'公司四','user111','bb' union all
select 5,'公司五','user114','bb' union all
select 6,'公司六','user008','cc'
--> 测试数据: #ProdTable
if object_id('tempdb.dbo.#ProdTable') is not null drop table #ProdTable
create table #ProdTable (ID int,公司ID int,产品名称 nvarchar(5))
insert into #ProdTable
select 1,1,'A产品' union all
select 2,1,'B产品' union all
select 3,1,'C产品' union all
select 4,2,'A产品' union all
select 5,3,'C产品' union all
select 6,3,'D产品'
--> 测试数据: #CompanyAdsTable
if object_id('tempdb.dbo.#CompanyAdsTable') is not null drop table #CompanyAdsTable
create table #CompanyAdsTable (ID int,关键字名词 varchar(5),公司ID int,排序显示位置 int)
insert into #CompanyAdsTable
select 1,'A产品',3,1 union all
select 2,'E产品',3,1 union all
select 3,'B产品',5,1 union all
select 4,'A产品',6,2
declare @s varchar(1000)
set @s = 'A产品'
select *,@s from
(select distinct 公司ID from #ProdTable where 产品名称=@s) a
left join #companyTable b on b.id = a.公司ID
order by
isnull((select rtrim(min(排序显示位置)) from #CompanyAdsTable where 关键字名词 = @s and 公司ID = a.公司ID),'a'),
(select 会员排序 from #userRole c,#userTable d where c.id=d.会员角色类型 and d.会员帐号 = b.useID) desc
/*
公司ID ID 公司名称 useID 地址
----------- ----------- ------ ------- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 2 公司二 user002 aa A产品
1 1 公司一 user001 aa A产品
*/