我在数据库MySQL中有一个表SignRegister其中注册了对网站区域的访问。
例如。:

mysql> SELECT
    id,
    `Sign`,
    `DateSign`,
    `HourSign`,
    `AccountSign`
FROM
    SignRegister
WHERE
`AccountSign` IN ('4041002')
ORDER BY
    `DateSign`,
    `HourSign` ASC;

+----+------+------------+----------+-------------+
| id | Sign | DateSign   | HourSign | AccountSign |
+----+------+------------+----------+-------------+
| 18 | B    | 2015-10-29 | 15:01:34 | 4041002     |
| 16 | L    | 2015-10-29 | 15:03:29 | 4041002     |
| 19 | B    | 2015-10-29 | 17:35:44 | 4041002     |
| 20 | B    | 2015-10-29 | 17:49:22 | 4041002     |
+----+------+------------+----------+-------------+
4 rows in set

现在我需要同样的AccountSign删除一小时内包含的访问,并且Sign值不是T值。
例如,对于AccountSign4041002,我需要删除Id number16和20的行:
+----+------+------------+----------+-------------+
| id | Sign | DateSign   | HourSign | AccountSign |
+----+------+------------+----------+-------------+
| 18 | B    | 2015-10-29 | 15:01:34 | 4041002     |
| 19 | B    | 2015-10-29 | 17:35:44 | 4041002     |
+----+------+------------+----------+-------------+

对于选择这些行,我尝试了此查询:
mysql> SELECT
    id,
    `Sign`,
    `DateSign`,
    `HourSign`,
    `AccountSign`,
    TIMEDIFF(a.`HourSign`, b.`HourSign`) AS strDiff1
FROM
    SignRegister a
JOIN SignRegister ON a.`AccountSign` = b.`AccountSign`
AND (
    TIMEDIFF(a.`HourSign`, b.`HourSign`) BETWEEN '00:00:00'
    AND '01:00:00'
)
WHERE
`AccountSign` IN ('4041002')
AND `Sign` IN ('B', 'L')
AND (
    TIMEDIFF(a.`HourSign`, b.`HourSign`) BETWEEN '00:00:00'
    AND '01:00:00'
)
ORDER BY
    `DateSign`,
    `HourSign` ASC;

+----+------+------------+----------+-------------+----------+
| id | Sign | DateSign   | HourSign | AccountSign | strDiff1 |
+----+------+------------+----------+-------------+----------+
| 18 | B    | 2015-10-29 | 15:01:34 | 4041002     | 00:00:00 |
| 16 | L    | 2015-10-29 | 15:03:29 | 4041002     | 00:00:00 |
| 16 | L    | 2015-10-29 | 15:03:29 | 4041002     | 00:01:55 |
| 19 | B    | 2015-10-29 | 17:35:44 | 4041002     | 00:00:00 |
| 20 | B    | 2015-10-29 | 17:49:22 | 4041002     | 00:13:38 |
| 20 | B    | 2015-10-29 | 17:49:22 | 4041002     | 00:00:00 |
+----+------+------------+----------+-------------+----------+
6 rows in set

在这种情况下,我找到了ID number16和strDiff100:00:00不同的AccountSign20。
现在对其他AccountSign尝试相同的代码,例如:
mysql> SELECT
    id,
    `Sign`,
    `DateSign`,
    `HourSign`,
    `AccountSign`
FROM
    SignRegister
WHERE
`AccountSign` IN ('4042801')
ORDER BY
    `DateSign`,
    `HourSign` ASC;


+----+------+------------+----------+-------------+
| id | Sign | DateSign   | HourSign | AccountSign |
+----+------+------------+----------+-------------+
| 83 | T    | 2015-10-29 | 06:31:02 | 4042801     |
| 85 | B    | 2015-10-29 | 06:34:49 | 4042801     |
+----+------+------------+----------+-------------+
2 rows in set

在这种情况下,我不会在一小时内删除相同的T访问,因为第一个访问是signID number值。
但当我尝试选择这些行时:
mysql> SELECT
    id,
    `Sign`,
    `DateSign`,
    `HourSign`,
    `AccountSign`,
    TIMEDIFF(a.`HourSign`, b.`HourSign`) AS strDiff1
FROM
    SignRegister a
JOIN SignRegister ON a.`AccountSign` = b.`AccountSign`
AND (
    TIMEDIFF(a.`HourSign`, b.`HourSign`) BETWEEN '00:00:00'
    AND '01:00:00'
)
WHERE
`AccountSign` IN ('4042801')
AND `Sign` IN ('B', 'L')
AND (
    TIMEDIFF(a.`HourSign`, b.`HourSign`) BETWEEN '00:00:00'
    AND '01:00:00'
)
ORDER BY
    `DateSign`,
    `HourSign` ASC;


+----+------+------------+----------+-------------+----------+
| id | Sign | DateSign   | HourSign | AccountSign | strDiff1 |
+----+------+------------+----------+-------------+----------+
| 85 | B    | 2015-10-29 | 06:34:49 | 4042801     | 00:03:47 |
| 85 | B    | 2015-10-29 | 06:34:49 | 4042801     | 00:00:00 |
+----+------+------------+----------+-------------+----------+
2 rows in set

查询定位不可删除的Sign85,因为第一个TAccountSign值。
请帮帮我。
提前谢谢你。
编辑#01
对于表中的SignRegister4042801,我已经记住了这些访问:
+----+------+------------+----------+-------------+
| id | Sign | DateSign   | HourSign | AccountSign |
+----+------+------------+----------+-------------+
| 83 | T    | 2015-10-29 | 06:31:02 | 4042801     |
| 85 | B    | 2015-10-29 | 06:34:49 | 4042801     |
+----+------+------------+----------+-------------+

在这种情况下,AccountSign4042801的第一个访问具有Sign等于T值。
当存在这种情况时,不必在一小时内删除访问,因为Sign等于T值。

最佳答案

添加到WHERE子句:

AND
b.Sign IN ('B', 'L')
AND
a.id <> b.id

两个“表”(左SignRegister和右SignRegister)都必须有符号限制,并且记录不能与它们自己连接(为方便起见,可选)。

10-07 12:31