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

查询数据的有关问题

2012-11-20 
查询数据的问题有这么一张表,数据类似如下:col1 col2 col3a1b1c1a1b1c1a1b2c3a2b2c3a2b2c4a2b3c5a3b4c6现

查询数据的问题
有这么一张表,数据类似如下:
col1 col2 col3
a1 b1 c1
a1 b1 c1
a1 b2 c3
a2 b2 c3
a2 b2 c4
a2 b3 c5
a3 b4 c6


现在有如下要求:
以col1为主,查询一张表,表中只要求col1列中的数据不能重复,请问如何查询


[解决办法]

SQL code
------------------------------ Author  :fredrickhu(小F,向高手学习)-- Date    :2011-08-11 21:16:57-- 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)--------------------------------> 测试数据:[tb]if object_id('[tb]') is not null drop table [tb]go create table [tb]([col1] varchar(2),[col2] varchar(2),[col3] varchar(2))insert [tb]select 'a1','b1','c1' union allselect 'a1','b1','c1' union allselect 'a1','b2','c3' union allselect 'a2','b2','c3' union allselect 'a2','b2','c4' union allselect 'a2','b3','c5' union allselect 'a3','b4','c6'--------------开始查询--------------------------select *  from  tb t  where  not exists(select 1 from tb where (col1=t.col1 and col2>t.col2) or (col1=t.col1  and col3>t.col3))----------------结果----------------------------/* col1 col2 col3---- ---- ----a1   b2   c3a2   b3   c5a3   b4   c6(3 行受影响)*/ 

热点排行
Bad Request.