如何查询匹配的记录集

时间:2014-04-13 03:55:12

标签: mysql sql

我正在尝试查询以查找居住在同一地址的人。但我的SQL似乎失败了,不知道为什么。

mysql> SELECT * FROM polished1 WHERE LName = 'BENDICH';                                                        +------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| id   | StateVoterID | Title | FName  | MName | LName   | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| 2790 | 72253   |       | ARNOLD | J     | BENDICH |            | 5         | M      | 1754     |           | NE                | 2ND      | ST        |                    |             | 0          | SEATTLE | WA       | 98115      |       2071 | 43           | 7                   |                       |       |       |       |       |          |         | 10/1/1965   | P                | 2/12/2013    | A         | NULL       |
| 2791 | 72253   |       | JUDITH | E     | BENDICH |            | 1         | F      | 1754     |           | NE                | 2ND      | ST        |                    |             | 0          | SEATTLE | WA       | 98115      |       2071 | 43           | 7                   |                       |       |       |       |       |          |         | 10/1/1965   | P                | 2/12/2013    | A         | NULL       |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
2 rows in set (0.00 sec)

mysql> SELECT *, COUNT(*) c FROM polished1 WHERE LName = 'Bendich' GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode HAVING c > 1;
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| id   | StateVoterID | Title | FName  | MName | LName   | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode | c |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| 2790 | 72253    |       | ARNOLD | J     | BENDICH |            | 5         | M      | 1754     |           | NE                | 2ND      | ST        |                    |             | 0          | SEATTLE | WA       | 98115      |       2071 | 43           | 7                   |                       |       |       |       |       |          |         | 10/1/1965   | P                | 2/12/2013    | A         | NULL       | 2 |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
1 row in set (0.01 sec)

http://bpaste.net/show/UWC8wdOWMbFEQcT7eV6f/

1 个答案:

答案 0 :(得分:1)

select p.*
from polished1 p JOIN
   (
    SELECT *, COUNT(*) c 
    FROM polished1 
    WHERE LName = 'Bendich' 
    GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode 
    HAVING c > 1) filtered ON p.RegStNum=filtered.RegStNum 
and p.RegStName=filtered.RegStName 
and p.RegStType=filtered.RegStType 
and p.RegUnitType=filtered.RegUnitType 
and p.RegStPreDirection=filtered.RegStPreDirection 
and p.RegStPostDirection=filtered.RegStPostDirection 
and p.RegUnitNum=filtered.RegUnitNum 
and p.RegCity=filtered.RegCity 
and p.RegState=filtered.RegState 
and p.RegZipCode=filtered.RegZipCode 

您应首先检索count(*)> 1的地址,然后获取所有已加入的地址