日期:2014-05-19  浏览次数:20601 次

一个查询条件,求助!!!

假如数据库表里有省(province)、市(city)、县(town)三个级联字段,还有字段手机(telphone)、姓名(uername)、ID;


要查的结果:是多次出现的数据  

数据算多次出现的条件: 必须是 省+市+县 相同; 姓名 、手机任何一个相同即可。

------解决方案--------------------
我想楼主的问题是要找重复,1楼给的不是很正确。

楼主你恐怕得分为两次进行查询会更简单些,比如这个可以找出所有重名情况及重名的次数:
Select province, city, town, username, sum(*) as nums
From table
Group By province, city, town, username


然后再找电话号码重复的情况:
Select province, city, town, telphone, sum(*) as nums
From table
Group By province, city, town, telphone

------解决方案--------------------
2楼个人感觉给得不太对,你这样查表只能查出省+市+县 +任意一个成立的条件,这样如果有用户名和电话都相同的,你这样查就会有重复查询,不符合题意,而且所得的sum存在重合,存在意义的范围很小。
------解决方案--------------------
select * from table where province='' and city='' and town='' and (telphone='' or username= '')
查询省and市and县and电话或者用户名
------解决方案--------------------
麻烦看懂别人的问题再回答,
------解决方案--------------------
select *from table where id in
(
 select id from table where 
 (
province in(select province from table)
 and 
city in(select city from table)
 and 
town in(select town from table)
 )
 and 
 (
telphone in(select telphone from table)
 or
username in(select username from table)
 )
)
思路应该是这样子的,lz可以试试