首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请教如何写SQL

2012-01-28 
请问怎么写SQL?数据库结构表1字段detailsid chrtitle1 标题12 标题23 标题34 标题4表2字段chrimage liveid

请问怎么写SQL?
数据库结构
表1字段
detailsid chrtitle  
1 标题1
2 标题2
3 标题3
4 标题4
表2字段
chrimage liveid
1.jpg 1
2.jpg 1
3.jpg 1
4.jpg 2
5.jpg 3
6.jpg 4

查询出来结果 表2的chrimage不允许有重复



[解决办法]

SQL code
select m.* , n.* from tb1 m , tb2 nwhere m.detailsid = n.detailsid andn.chrimage = (select top 1 chrimage from tb2 where liveid = n.liveid order by chrimage)select m.* , n.* from tb1 m , tb2 nwhere m.detailsid = n.detailsid andn.chrimage = (select top 1 chrimage from tb2 where liveid = n.liveid order by chrimage desc)select m.* , n.* from tb1 m , tb2 nwhere m.detailsid = n.detailsid andn.chrimage = (select min(chrimage) from tb2 where liveid = n.liveid)select m.* , n.* from tb1 m , tb2 nwhere m.detailsid = n.detailsid andn.chrimage = (select max(chrimage) from tb2 where liveid = n.liveid)select m.* , n.* from tb1 m , tb2 nwhere m.detailsid = n.detailsid andnot exists (select 1 from tb2 where liveid = n.liveid and chrimage < n.chrimage)select m.* , n.* from tb1 m , tb2 nwhere m.detailsid = n.detailsid andnot exists (select 1 from tb2 where liveid = n.liveid and chrimage > n.chrimage) 

热点排行