62,046
社区成员
发帖
与我相关
我的任务
分享
select * from View_ShopMedicine where exists ( select MedicineId,ShopId,Standard from ShopMedicine sm where sm.MedicineId = View_ShopMedicine.MedicineId and sm.ShopId = View_ShopMedicine.ShopId and sm.Standard=View_ShopMedicine.ShopMedicineStandard group by MedicineId,ShopId,Standard having(COUNT(*)) >1)
if object_id('[View_ShopMedicine]') is not null drop table [View_ShopMedicine]
create table [View_ShopMedicine] (MedicineId int,ShopId int,ShopMedicineStandard int)
insert into [View_ShopMedicine]
select 1,1,1 union all
select 2,2,2 union all
select 3,3,3 union all
select 1,1,1 union all
select 2,2,2 union all
select 4,4,4
if object_id('[ShopMedicine]') is not null drop table [ShopMedicine]
create table [ShopMedicine] (MedicineId int,ShopId int,Standard int)
insert into [ShopMedicine]
select 1,1,1 union all
select 3,3,3 union all
select 1,1,1 union all
select 2,2,2 union all
select 4,4,4
--你原来的写法
select * from
View_ShopMedicine
where exists
(
select MedicineId,ShopId,Standard from ShopMedicine sm
where sm.MedicineId = View_ShopMedicine.MedicineId
and sm.ShopId = View_ShopMedicine.ShopId
and sm.Standard=View_ShopMedicine.ShopMedicineStandard
group by MedicineId,ShopId,Standard having(COUNT(*)) >1
)
--你的结果
/*
MedicineId ShopId ShopMedicineStandard
----------- ----------- --------------------
1 1 1
1 1 1
*/
select * from
View_ShopMedicine
where exists
(
select MedicineId,ShopId,Standard from ShopMedicine sm
where sm.MedicineId = View_ShopMedicine.MedicineId
and sm.ShopId = View_ShopMedicine.ShopId
and sm.Standard=View_ShopMedicine.ShopMedicineStandard
group by MedicineId,ShopId,Standard having(COUNT(*)) >1
)
--不知道你要实现一个什么样的效果