将表中某一列中字段值相同的查询出来
例子:将 sms_user_organ 的USER_ID列中 user_id值相同的查询出来
表名:sms_user_organ
USER_ID ORGAN_ID
4427 549
4428 7
4428 9
4429 394
查询语句
SELECT * FROM sms_user_organ A
WHERE (SELECT COUNT(*) FROM sms_user_organ WHERE user_id=A.user_id)>1
查询结果
USER_ID ORGAN_ID
4428 7
4428 9
或写查询语句
SELECT A.User_Id FROM sms_user_organ A group by A.User_Id having count(*)>1
结果
USER_ID
4428
4428
例子:将 sms_user_organ 的USER_ID列中 user_id值相同的查询出来
表名:sms_user_organ
USER_ID ORGAN_ID
4427 549
4428 7
4428 9
4429 394
查询语句
SELECT * FROM sms_user_organ A
WHERE (SELECT COUNT(*) FROM sms_user_organ WHERE user_id=A.user_id)>1
查询结果
USER_ID ORGAN_ID
4428 7
4428 9
或写查询语句
SELECT A.User_Id FROM sms_user_organ A group by A.User_Id having count(*)>1
结果
USER_ID
4428
4428
修改后:
SELECT * FROM test A
WHERE (SELECT COUNT(*) FROM test WHERE number=A.number)=1
SELECT COUNT(*) FROM test 统计表中的所有行数目之和
SELECT A.number FROM test A group by A.number having count(*)>1 查出number列的重复元素
SELECT A.number FROM test A group by A.number having count(*)=1 查出number列的所有非重复元素
SELECT A.number FROM test A group by A.number 查出number列的所有元素 重复的值出现一次
SELECT number FROM test 查出表test中列名为number的所有元素。