MySql查询重复字段

时间:2014-03-20 10:30:16

标签: php mysql sql

我的sql表包含了内容:

id | joined              | ip
---------------------------------------
 1 | 2014-03-20 10:01:51 | 214.254.22.1
 2 | 2014-03-20 11:01:51 | 214.254.2.1
 5 | 2014-03-20 12:01:51 | 214.254.12.1
 8 | 2014-03-20 13:01:51 | 214.254.22.1

例如,我想在joined之后找到2014-03-20 10:00:00的所有结果,ip在所有记录中找到多次

所以在这种情况下我的结果应该是:

 1 | 2014-03-20 10:01:51 | 214.254.22.1

因为它在10:00:00之后加入,并且在数据库中找到了ip 214.254.22.1 2次。

这种查询是否可行?

4 个答案:

答案 0 :(得分:1)

试试这个:

mysql> create table `t` (`id`  int, `joined` datetime, `ip` char(20));
Query OK, 0 rows affected (0.43 sec)

mysql> insert into `t` values
    -> (1, '2014-03-20 10:01:51', '214.254.22.1'),
    -> (2, '2014-03-20 11:01:51', '214.254.2.1'),
    -> (5, '2014-03-20 12:01:51', '214.254.12.1'),
    -> (8, '2014-03-20 13:01:51', '214.254.22.1');
Query OK, 4 rows affected (0.12 sec)
Records: 4  Duplicates: 0  Warnings: 0

[回答] 查询:

mysql> select *, count(`ip`) as total_ip  
    -> from `t` 
    -> where joined > '2014-03-20 10:00:00' 
    -> group by ip 
    -> having total_ip > 1 ;
+------+---------------------+--------------+----------+
| id   | joined              | ip           | total_ip |
+------+---------------------+--------------+----------+
|    1 | 2014-03-20 10:01:51 | 214.254.22.1 |        2 |
+------+---------------------+--------------+----------+
1 row in set (0.00 sec)

答案 1 :(得分:0)

您可以使用Having子句过滤特定聚合,如下所示:

select
    id,
    joined,
    count(ip)
from
    yourTable
where
    joined > yourDate
group by
    id,
    joined
having
    count(ip) > 1

您可以在此处找到有关此内容的更多信息:http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html

答案 2 :(得分:0)

试试这个:

SELECT id, joined, ip, count(ip) 
FROM tablename
WHERE joined > date  
HAVING count(ip) > 2

答案 3 :(得分:0)

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id INT NOT NULL
,joined              DATETIME
,ip VARCHAR(20) NOT NULL
);

INSERT INTO my_table VALUES
( 1 ,'2014-03-20 10:01:51','214.254.22.1'),
( 2 ,'2014-03-20 11:01:51','214.254.2.1'),
( 5 ,'2014-03-20 12:01:51','214.254.12.1'),
( 8 ,'2014-03-20 13:01:51','214.254.22.1');

对于您的特定数据集,以下符号就足够了......

SELECT x.* 
  FROM my_table x 
  JOIN my_table y 
    ON y.ip = x.ip 
   AND y.joined > x.joined 
 WHERE x.joined > '2014-03-20 10:00:00';
+----+---------------------+--------------+
| id | joined              | ip           |
+----+---------------------+--------------+
|  1 | 2014-03-20 10:01:51 | 214.254.22.1 |
+----+---------------------+--------------+

如果ip有可能出现多次,你可以考虑将其包装在子查询中......

SELECT a.* 
  FROM my_table a
  JOIN
     ( SELECT MIN(x.id) min_id 
         FROM my_table x 
         JOIN my_table y 
           ON y.ip = x.ip 
          AND y.joined > x.joined 
        WHERE x.joined > '2014-03-20 10:00:00' 
        GROUP 
           BY x.ip
     ) b
    ON b.min_id = a.id;
+----+---------------------+--------------+
| id | joined              | ip           |
+----+---------------------+--------------+
|  1 | 2014-03-20 10:01:51 | 214.254.22.1 |
+----+---------------------+--------------+