mess
id(递增/int) name(姓名/nvarchar) mess(留言内容/nvarchar)
1 AA **
2 BB **
3 AA **
4 AA **
5 BB **
6 EE **
7 FF **
我想查询出的结果是
1 AA **
2 BB **
6 CC **
7 DD **
请高手指教一下应该怎么写这个SQL语句呢(兄弟们希望你的SQL语句测试后在发上来好么 谢谢)
(
select distinct (name) form mess
select distinct(*) from mess
请不要发以上2条类似的sql语句,我测试过,以上两条语句全部不行,
)
39 个解决方案
#1
select * from tb where not exists
(select 1 from tb where name=t.name and id<t.id)
(select 1 from tb where name=t.name and id<t.id)
#2
select * from mess where not exists
(select 1 from mess where name=t.name and id<t.id)
(select 1 from mess where name=t.name and id<t.id)
#3
mark
#4
结果应该是:
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
#5
select * from tb wher id in (select distinct (name),id form mess)
#6
haonanernet(与时俱进) ( ) 信誉:100 2007-08-21 10:32:13 得分: 0
结果应该是:
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
=================================================================
恩呵呵 写错了
结果应该是:
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
=================================================================
恩呵呵 写错了
#7
少了一个e\
select * from tb where id in (select id from(select distinct (name),id form mess))
select * from tb where id in (select id from(select distinct (name),id form mess))
#8
挺奇怪的
select distinct name from tt 我这样就行
AA
BB
select distinct name from tt 我这样就行
AA
BB
#9
select distinct (name),** from mess
#10
zhqs1000(子鱼) ( ) 信誉:100 2007-08-21 10:37:17 得分: 0
挺奇怪的
select distinct name from tt 我这样就行
AA
BB
Top
chshjking(金少) ( ) 信誉:100 2007-08-21 10:38:04 得分: 0
select distinct (name),** from mess
Top
=============================
这样的形式只能显示一列字段 我要整表的字段
挺奇怪的
select distinct name from tt 我这样就行
AA
BB
Top
chshjking(金少) ( ) 信誉:100 2007-08-21 10:38:04 得分: 0
select distinct (name),** from mess
Top
=============================
这样的形式只能显示一列字段 我要整表的字段
#11
select * from [mess]
where id<=(select min(id) from [mess]
group by name )
可能不对,你试试
where id<=(select min(id) from [mess]
group by name )
可能不对,你试试
#12
Select * From mess T
Where (Select Count(A) From mess Where name= T.name And id < T.id) =0
Order By id, name
Where (Select Count(A) From mess Where name= T.name And id < T.id) =0
Order By id, name
#13
select * from mess as m1 where not exists
(select 1 from mess where name=m1.name and id<m1.id)
(select 1 from mess where name=m1.name and id<m1.id)
#14
下面这个少了个别名
------------
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
------------
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
#15
不用这样麻烦吧
select distinct ([name]),[id],[mess] from mess
没有问题
select distinct ([name]),[id],[mess] from mess
没有问题
#16
select * from mess
where id in (select min(id) from mess group by name )
where id in (select min(id) from mess group by name )
#17
--方法一
Select * From mess A Where Not Exists(Select id From mess Where name = A.name And id < A.id)
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
--方法三
Select * From mess A Where id = (Select Min(id) From mess Where name = A.name)
--方法四
Select A.* From mess A
Inner Join
(Select name, Min(id) As id From mess Group By name) B
On A.name = B.name And A.id = B.id
Select * From mess A Where Not Exists(Select id From mess Where name = A.name And id < A.id)
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
--方法三
Select * From mess A Where id = (Select Min(id) From mess Where name = A.name)
--方法四
Select A.* From mess A
Inner Join
(Select name, Min(id) As id From mess Group By name) B
On A.name = B.name And A.id = B.id
#18
andy888666(恋上未来的你) ( ) 信誉:100 2007-08-21 10:51:57 得分: 0
不用这样麻烦吧
select distinct ([name]),[id],[mess] from mess
没有问题
----------
說"沒有問題"前,最好自己先測試下。
不用这样麻烦吧
select distinct ([name]),[id],[mess] from mess
没有问题
----------
說"沒有問題"前,最好自己先測試下。
#19
SELECT a.* FROM mess a
WHERE NOT EXISTS (SELECT 1 FROM mess b WHERE a.name = b.name AND a.id > b.id)
WHERE NOT EXISTS (SELECT 1 FROM mess b WHERE a.name = b.name AND a.id > b.id)
#20
--方法一:
Select * From mess T
Where (Select Count(name) From mess Where name= T.name And id < T.id) =0
Order By id, name
--方法二:
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
--方法三:
Select * From mess T
Where id In (Select TOP 1 id From A Where name = T.name Order By id asc)
Order By name, id asc
Select * From mess T
Where (Select Count(name) From mess Where name= T.name And id < T.id) =0
Order By id, name
--方法二:
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
--方法三:
Select * From mess T
Where id In (Select TOP 1 id From A Where name = T.name Order By id asc)
Order By name, id asc
#21
做人要厚道!
#22
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name
on a.id=b.id and a.name=b.name
#23
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
on a.id=b.id and a.name=b.name order by b.name asc
#24
haonanernet(与时俱进), 你這個應該是看到我在取某個TOP N的帖子中的回復吧。
這裡是取最小的一個,所以沒必要使用這三種方法。
因為ID是自增列,所以可以使用我上面寫的四種方法,更簡單些。
這裡是取最小的一個,所以沒必要使用這三種方法。
因為ID是自增列,所以可以使用我上面寫的四種方法,更簡單些。
#25
收藏
#26
shoushii(守矢) ( ) 信誉:92 2007-08-21 11:02:32 得分: 0
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
----------
語法錯誤
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
----------
語法錯誤
#27
paoluo(一天到晚游泳的鱼(學習.NET中))
----------------
我正奇怪你怎么还没有贴上呢?哈哈.
----------------
我正奇怪你怎么还没有贴上呢?哈哈.
#28
這個ID是特殊的,所以有這四種方法,簡單些。
你貼的那三種是通常是用來取TOP N的。
你貼的那三種是通常是用來取TOP N的。
#29
恩,其实我最先想到的就是这个方法:
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
也就是你4种方法中的方法一
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
也就是你4种方法中的方法一
#30
以下以aa为例进行说明:
如果name=aa对应的mess都是相同的,可以
select name,mess from table group by name,mess;
如果想要查询是否有name,mess重复的数据,可以
select name,mess,count(*) from table group by name,mess
having count(*)=1;--等于1不重复,>1说明有重复
如果name=aa对应的mess是不相同的,可以
select name,mess from table where id=(select min(id) from table where name='aa')
and name='aa';
如果想快速删除重复数据,可以
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.name= E.name);
如果name=aa对应的mess都是相同的,可以
select name,mess from table group by name,mess;
如果想要查询是否有name,mess重复的数据,可以
select name,mess,count(*) from table group by name,mess
having count(*)=1;--等于1不重复,>1说明有重复
如果name=aa对应的mess是不相同的,可以
select name,mess from table where id=(select min(id) from table where name='aa')
and name='aa';
如果想快速删除重复数据,可以
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.name= E.name);
#31
樓主用的是exists的方法?
這個ID是自增列,這個語句應該效率也還可以
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
這個ID是自增列,這個語句應該效率也還可以
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
#32
这条可以
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
#33
jayma555(踩小姑娘的老蘑菇) ( ) 信誉:100 2007-08-21 11:25:29 得分: 0
这条可以
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
-----------
你測試了沒?
都語法錯誤了,還可以。
这条可以
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
-----------
你測試了沒?
都語法錯誤了,還可以。
#34
to paoluo(一天到晚游泳的鱼(學習.NET中)) :
感觉你太热心了,别人的回复都要去测试一下的!
感觉你太热心了,别人的回复都要去测试一下的!
#35
kinglht(爱新觉罗至尊宝) ( ) 信誉:100 2007-08-21 11:29:28 得分: 0
to paoluo(一天到晚游泳的鱼(學習.NET中)) :
感觉你太热心了,别人的回复都要去测试一下的!
----------
其實你只需要放入查詢分析器中執行下即可。
而那個語句,很明顯的可以看到語法錯誤,即使不執行也會知道是錯誤的。
to paoluo(一天到晚游泳的鱼(學習.NET中)) :
感觉你太热心了,别人的回复都要去测试一下的!
----------
其實你只需要放入查詢分析器中執行下即可。
而那個語句,很明顯的可以看到語法錯誤,即使不執行也會知道是錯誤的。
#36
楼上的,有的人回复的用的数据库是不一样的,可能有些地方的语法稍有出入,况且这里回复的基本都是凭记忆写出来的,难免会有落关键字等错误,你用你的数据库测试有什么意义呢!
#37
kinglht(爱新觉罗至尊宝) ( ) 信誉:100 2007-08-21 11:42:45 得分: 0
楼上的,有的人回复的用的数据库是不一样的,可能有些地方的语法稍有出入,况且这里回复的基本都是凭记忆写出来的,难免会有落关键字等错误,你用你的数据库测试有什么意义呢!
--------
基本語法錯誤,用什麼數據庫測試都是一樣的。
注意,是基本語法錯誤。
就算是憑記憶寫的,有錯誤很正常,但是還是要盡量寫正確的。
而且,我指出語法錯誤,也沒有問題吧。
再說了,寫個錯誤的在那,沒人會拿去用的。
楼上的,有的人回复的用的数据库是不一样的,可能有些地方的语法稍有出入,况且这里回复的基本都是凭记忆写出来的,难免会有落关键字等错误,你用你的数据库测试有什么意义呢!
--------
基本語法錯誤,用什麼數據庫測試都是一樣的。
注意,是基本語法錯誤。
就算是憑記憶寫的,有錯誤很正常,但是還是要盡量寫正確的。
而且,我指出語法錯誤,也沒有問題吧。
再說了,寫個錯誤的在那,沒人會拿去用的。
#38
再說了,那個錯誤那麼明顯,即使不用測試,也可以知道有錯誤的。
#39
$res=$c->distinct(name)->where('type=1')->select();
不行啊,谁能帮忙解决下
不行啊,谁能帮忙解决下
#1
select * from tb where not exists
(select 1 from tb where name=t.name and id<t.id)
(select 1 from tb where name=t.name and id<t.id)
#2
select * from mess where not exists
(select 1 from mess where name=t.name and id<t.id)
(select 1 from mess where name=t.name and id<t.id)
#3
mark
#4
结果应该是:
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
#5
select * from tb wher id in (select distinct (name),id form mess)
#6
haonanernet(与时俱进) ( ) 信誉:100 2007-08-21 10:32:13 得分: 0
结果应该是:
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
=================================================================
恩呵呵 写错了
结果应该是:
-----------
1 AA **
2 BB **
6 EE **
7 FF **
吧?
=================================================================
恩呵呵 写错了
#7
少了一个e\
select * from tb where id in (select id from(select distinct (name),id form mess))
select * from tb where id in (select id from(select distinct (name),id form mess))
#8
挺奇怪的
select distinct name from tt 我这样就行
AA
BB
select distinct name from tt 我这样就行
AA
BB
#9
select distinct (name),** from mess
#10
zhqs1000(子鱼) ( ) 信誉:100 2007-08-21 10:37:17 得分: 0
挺奇怪的
select distinct name from tt 我这样就行
AA
BB
Top
chshjking(金少) ( ) 信誉:100 2007-08-21 10:38:04 得分: 0
select distinct (name),** from mess
Top
=============================
这样的形式只能显示一列字段 我要整表的字段
挺奇怪的
select distinct name from tt 我这样就行
AA
BB
Top
chshjking(金少) ( ) 信誉:100 2007-08-21 10:38:04 得分: 0
select distinct (name),** from mess
Top
=============================
这样的形式只能显示一列字段 我要整表的字段
#11
select * from [mess]
where id<=(select min(id) from [mess]
group by name )
可能不对,你试试
where id<=(select min(id) from [mess]
group by name )
可能不对,你试试
#12
Select * From mess T
Where (Select Count(A) From mess Where name= T.name And id < T.id) =0
Order By id, name
Where (Select Count(A) From mess Where name= T.name And id < T.id) =0
Order By id, name
#13
select * from mess as m1 where not exists
(select 1 from mess where name=m1.name and id<m1.id)
(select 1 from mess where name=m1.name and id<m1.id)
#14
下面这个少了个别名
------------
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
------------
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
#15
不用这样麻烦吧
select distinct ([name]),[id],[mess] from mess
没有问题
select distinct ([name]),[id],[mess] from mess
没有问题
#16
select * from mess
where id in (select min(id) from mess group by name )
where id in (select min(id) from mess group by name )
#17
--方法一
Select * From mess A Where Not Exists(Select id From mess Where name = A.name And id < A.id)
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
--方法三
Select * From mess A Where id = (Select Min(id) From mess Where name = A.name)
--方法四
Select A.* From mess A
Inner Join
(Select name, Min(id) As id From mess Group By name) B
On A.name = B.name And A.id = B.id
Select * From mess A Where Not Exists(Select id From mess Where name = A.name And id < A.id)
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
--方法三
Select * From mess A Where id = (Select Min(id) From mess Where name = A.name)
--方法四
Select A.* From mess A
Inner Join
(Select name, Min(id) As id From mess Group By name) B
On A.name = B.name And A.id = B.id
#18
andy888666(恋上未来的你) ( ) 信誉:100 2007-08-21 10:51:57 得分: 0
不用这样麻烦吧
select distinct ([name]),[id],[mess] from mess
没有问题
----------
說"沒有問題"前,最好自己先測試下。
不用这样麻烦吧
select distinct ([name]),[id],[mess] from mess
没有问题
----------
說"沒有問題"前,最好自己先測試下。
#19
SELECT a.* FROM mess a
WHERE NOT EXISTS (SELECT 1 FROM mess b WHERE a.name = b.name AND a.id > b.id)
WHERE NOT EXISTS (SELECT 1 FROM mess b WHERE a.name = b.name AND a.id > b.id)
#20
--方法一:
Select * From mess T
Where (Select Count(name) From mess Where name= T.name And id < T.id) =0
Order By id, name
--方法二:
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
--方法三:
Select * From mess T
Where id In (Select TOP 1 id From A Where name = T.name Order By id asc)
Order By name, id asc
Select * From mess T
Where (Select Count(name) From mess Where name= T.name And id < T.id) =0
Order By id, name
--方法二:
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
--方法三:
Select * From mess T
Where id In (Select TOP 1 id From A Where name = T.name Order By id asc)
Order By name, id asc
#21
做人要厚道!
#22
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name
on a.id=b.id and a.name=b.name
#23
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
on a.id=b.id and a.name=b.name order by b.name asc
#24
haonanernet(与时俱进), 你這個應該是看到我在取某個TOP N的帖子中的回復吧。
這裡是取最小的一個,所以沒必要使用這三種方法。
因為ID是自增列,所以可以使用我上面寫的四種方法,更簡單些。
這裡是取最小的一個,所以沒必要使用這三種方法。
因為ID是自增列,所以可以使用我上面寫的四種方法,更簡單些。
#25
收藏
#26
shoushii(守矢) ( ) 信誉:92 2007-08-21 11:02:32 得分: 0
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
----------
語法錯誤
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
----------
語法錯誤
#27
paoluo(一天到晚游泳的鱼(學習.NET中))
----------------
我正奇怪你怎么还没有贴上呢?哈哈.
----------------
我正奇怪你怎么还没有贴上呢?哈哈.
#28
這個ID是特殊的,所以有這四種方法,簡單些。
你貼的那三種是通常是用來取TOP N的。
你貼的那三種是通常是用來取TOP N的。
#29
恩,其实我最先想到的就是这个方法:
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
也就是你4种方法中的方法一
select * from mess t where not exists
(select 1 from mess where name=t.name and id<t.id)
也就是你4种方法中的方法一
#30
以下以aa为例进行说明:
如果name=aa对应的mess都是相同的,可以
select name,mess from table group by name,mess;
如果想要查询是否有name,mess重复的数据,可以
select name,mess,count(*) from table group by name,mess
having count(*)=1;--等于1不重复,>1说明有重复
如果name=aa对应的mess是不相同的,可以
select name,mess from table where id=(select min(id) from table where name='aa')
and name='aa';
如果想快速删除重复数据,可以
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.name= E.name);
如果name=aa对应的mess都是相同的,可以
select name,mess from table group by name,mess;
如果想要查询是否有name,mess重复的数据,可以
select name,mess,count(*) from table group by name,mess
having count(*)=1;--等于1不重复,>1说明有重复
如果name=aa对应的mess是不相同的,可以
select name,mess from table where id=(select min(id) from table where name='aa')
and name='aa';
如果想快速删除重复数据,可以
DELETE FROM table E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM table X
WHERE X.name= E.name);
#31
樓主用的是exists的方法?
這個ID是自增列,這個語句應該效率也還可以
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
這個ID是自增列,這個語句應該效率也還可以
--方法二
Select * From mess A Where id In (Select Min(id) From mess Group By name)
#32
这条可以
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
#33
jayma555(踩小姑娘的老蘑菇) ( ) 信誉:100 2007-08-21 11:25:29 得分: 0
这条可以
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
-----------
你測試了沒?
都語法錯誤了,還可以。
这条可以
select * from mess inner join (select id,distinct(name) from mess group by name) b
on a.id=b.id and a.name=b.name order by b.name asc
-----------
你測試了沒?
都語法錯誤了,還可以。
#34
to paoluo(一天到晚游泳的鱼(學習.NET中)) :
感觉你太热心了,别人的回复都要去测试一下的!
感觉你太热心了,别人的回复都要去测试一下的!
#35
kinglht(爱新觉罗至尊宝) ( ) 信誉:100 2007-08-21 11:29:28 得分: 0
to paoluo(一天到晚游泳的鱼(學習.NET中)) :
感觉你太热心了,别人的回复都要去测试一下的!
----------
其實你只需要放入查詢分析器中執行下即可。
而那個語句,很明顯的可以看到語法錯誤,即使不執行也會知道是錯誤的。
to paoluo(一天到晚游泳的鱼(學習.NET中)) :
感觉你太热心了,别人的回复都要去测试一下的!
----------
其實你只需要放入查詢分析器中執行下即可。
而那個語句,很明顯的可以看到語法錯誤,即使不執行也會知道是錯誤的。
#36
楼上的,有的人回复的用的数据库是不一样的,可能有些地方的语法稍有出入,况且这里回复的基本都是凭记忆写出来的,难免会有落关键字等错误,你用你的数据库测试有什么意义呢!
#37
kinglht(爱新觉罗至尊宝) ( ) 信誉:100 2007-08-21 11:42:45 得分: 0
楼上的,有的人回复的用的数据库是不一样的,可能有些地方的语法稍有出入,况且这里回复的基本都是凭记忆写出来的,难免会有落关键字等错误,你用你的数据库测试有什么意义呢!
--------
基本語法錯誤,用什麼數據庫測試都是一樣的。
注意,是基本語法錯誤。
就算是憑記憶寫的,有錯誤很正常,但是還是要盡量寫正確的。
而且,我指出語法錯誤,也沒有問題吧。
再說了,寫個錯誤的在那,沒人會拿去用的。
楼上的,有的人回复的用的数据库是不一样的,可能有些地方的语法稍有出入,况且这里回复的基本都是凭记忆写出来的,难免会有落关键字等错误,你用你的数据库测试有什么意义呢!
--------
基本語法錯誤,用什麼數據庫測試都是一樣的。
注意,是基本語法錯誤。
就算是憑記憶寫的,有錯誤很正常,但是還是要盡量寫正確的。
而且,我指出語法錯誤,也沒有問題吧。
再說了,寫個錯誤的在那,沒人會拿去用的。
#38
再說了,那個錯誤那麼明顯,即使不用測試,也可以知道有錯誤的。
#39
$res=$c->distinct(name)->where('type=1')->select();
不行啊,谁能帮忙解决下
不行啊,谁能帮忙解决下