我在数据库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
值。例如,对于
AccountSign
4041002,我需要删除Id number
16和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 number
16和strDiff1
与00:00:00
不同的AccountSign
20。现在对其他
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
查询定位不可删除的
Sign
85,因为第一个T
是AccountSign
值。请帮帮我。
提前谢谢你。
编辑#01
对于表中的
SignRegister
4042801,我已经记住了这些访问:+----+------+------------+----------+-------------+
| id | Sign | DateSign | HourSign | AccountSign |
+----+------+------------+----------+-------------+
| 83 | T | 2015-10-29 | 06:31:02 | 4042801 |
| 85 | B | 2015-10-29 | 06:34:49 | 4042801 |
+----+------+------------+----------+-------------+
在这种情况下,
AccountSign
4042801的第一个访问具有Sign
等于T
值。当存在这种情况时,不必在一小时内删除访问,因为
Sign
等于T
值。 最佳答案
添加到WHERE子句:
AND
b.Sign IN ('B', 'L')
AND
a.id <> b.id
两个“表”(左SignRegister和右SignRegister)都必须有符号限制,并且记录不能与它们自己连接(为方便起见,可选)。