SQL语句,取某条字段有多个值的记录?

比如:table T1.
id(char) name(char) age(char)
1 n1 18
1 n2 19
1 n2 20
2 n2 18
2 n2 19
3 n3 18

请各位达人帮忙给出“查询出age(char)有多个值的id的记录”的语法,谢谢。

第1个回答  2011-08-29
SELECT *
from T1 t WHERE EXISTS (SELECT 1 FROM T1 where id = t.id and name = t.name and age <> t.age)
第2个回答  2015-10-15
select count(字段名) from 表 where 字段=“字段名”
第3个回答  2011-08-29
SELECT
age, COUNT( distinct id )
FROM
T1
GROUP BY
age
HAVING
COUNT( distinct id ) > 1追问

您的语句查出的是,每个age(char)分别出现多少次。与我的问题不符。

谢谢。

第4个回答  2011-08-29
select * from Tab as a where exists(select 1 from Tab where ID=a.ID and Age<>a.Age)追问

select * from Tab as a where exists(select 1 from Tab where ID=a.ID and Agea.Age)
这条专家回答给的语句,查询出是age出现大于一次的记录。继续追问一下,我想查,age出现具体次数的记录,比如出现三次的记录,或者4次的记录,该怎么查呀?

追答

select * from Tab as a where (select count(distinct Age) from Tab where ID=a.ID ) =3

--可以這樣寫,查出記錄3 這里自己加條件 如:>1

本回答被提问者采纳