select * from tb t where id = (select max(id) from tb where Residential = t.Residential ) and Largearea='LD-北京'
前台页面采用控件嵌套<asp:Repeater ID="rptLarge" runat="server"> <ItemTemplate> <div><input type="checkbox" /><%#Eval("LargeArea") %></div> <div> <asp:Repeater ID="rptSmall" runat="server"> <ItemTemplate> <input type="checkbox" /><%#Eval("Residential")%> </ItemTemplate> </asp:Repeater> </div> </ItemTemplate> </asp:Repeater> 数据查询分两步: 1.先查询出所有大区SELECT [LargeArea] From 表 GROUP BY [LargeArea] 2.根据大区选出小区SELECT DISTINCT [Residential] FROM 表 WHERE [LargeArea] = 大区 数据库中没有保存区域的表吗?若没有,设计上就有问题了...
用group by行不?那样子的话就可以不显示重复内容了
--不会啊 LD-北京的都有显示出来 create table #temp(id int,Residential varchar(50),Largearea varchar(50)) insert into #temp select 1,'大区经理','LD-北京' union all select 2,'RPM' ,'LD-北京' union all select 3,'大区经理','LD-北京' union all select 4,'北京EN-1','LD-北京' union all select 5,'北京EN-2','LD-北京' union all select 5,'沈阳LE-1','LD-北区' union all select 6,'北京LE' ,'LD-北京'select * from #temp t where id=(select max(id) from #temp where Residential=t.Residential) and Largearea='LD-北京' /* 2 RPM LD-北京 3 大区经理 LD-北京 4 北京EN-1 LD-北京 5 北京EN-2 LD-北京 6 北京LE LD-北京 */
select Residential from tb where largearea="LD_北京" group by Residential;
这- -
distinct
from tb t
where id = (select max(id) from tb where Residential = t.Residential )
and
Largearea='LD-北京'
<ItemTemplate>
<div><input type="checkbox" /><%#Eval("LargeArea") %></div>
<div>
<asp:Repeater ID="rptSmall" runat="server">
<ItemTemplate>
<input type="checkbox" /><%#Eval("Residential")%>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
数据查询分两步:
1.先查询出所有大区SELECT [LargeArea] From 表 GROUP BY [LargeArea]
2.根据大区选出小区SELECT DISTINCT [Residential] FROM 表 WHERE [LargeArea] = 大区
数据库中没有保存区域的表吗?若没有,设计上就有问题了...
--不会啊 LD-北京的都有显示出来
create table #temp(id int,Residential varchar(50),Largearea varchar(50))
insert into #temp
select 1,'大区经理','LD-北京' union all
select 2,'RPM' ,'LD-北京' union all
select 3,'大区经理','LD-北京' union all
select 4,'北京EN-1','LD-北京' union all
select 5,'北京EN-2','LD-北京' union all
select 5,'沈阳LE-1','LD-北区' union all
select 6,'北京LE' ,'LD-北京'select * from #temp t where id=(select max(id) from #temp where Residential=t.Residential) and Largearea='LD-北京'
/*
2 RPM LD-北京
3 大区经理 LD-北京
4 北京EN-1 LD-北京
5 北京EN-2 LD-北京
6 北京LE LD-北京
*/
如果没有可以用Distinct把所有区别查出来,从第二图上可以看到那是明显的group by
您这样只显示了第一条和最后一条 图片是查询出来的结果。
表里实际北京LE北京EN-2大区助理 大区经理 等字段 这个字段要是有重复的只显示一次。这才是我要的
您这样只查询了两个字段 。我要全部 根据这个字段来查询出 Residential 但是Residential重复的只显示一次
我把 max 换成了 min 可以了 谢谢您!
试一试这个。