使用 INNER JOIN 从两个表中删除数据
Posted
技术标签:
【中文标题】使用 INNER JOIN 从两个表中删除数据【英文标题】:Delete data from two tables with INNER JOIN 【发布时间】:2013-12-22 09:54:12 【问题描述】:我想根据datetime_lastactive
使用一个 SQL 查询从两个表中删除数据,并且如果 IP 地址与您自己的匹配。但是当我尝试以下 SQL 查询时收到此错误消息:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'INNER JOIN visitors_main WHERE information_ipaddress = '123.123.123.123' A' at line 2' in ...
DELETE FROM visitors_list
INNER JOIN visitors_main
WHERE information_ipaddress = :ipaddress
AND datetime_lastactive < NOW() - INTERVAL 3 HOUR
表格如下所示:
CREATE TABLE IF NOT EXISTS `visitors_list` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id_visitor` int(10) DEFAULT '0',
`id_user` int(10) DEFAULT '0',
`data_filename` text NOT NULL,
`data_filename_get` text NOT NULL,
`data_useragent` text NOT NULL,
`datetime_lastactive` datetime NOT NULL,
`information_ipaddress` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `visitors_main` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id_user` int(10) DEFAULT '0',
`data_coordinates` varchar(25) NOT NULL,
`datetime_firstvisit` datetime NOT NULL,
`checkbox_anonymous` tinyint(4) DEFAULT '0',
`checkbox_tiecoordinates` tinyint(4) DEFAULT '0',
`checkbox_nogps` tinyint(4) DEFAULT '0',
`information_ipaddress` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
我怎样才能做到这一点?
【问题讨论】:
尝试对表名使用反引号。 谢谢,但这并没有解决问题。我收到了同样的错误信息 在 WHERE 之前使用 ON 条件。 @Mihai... ON information_ipaddress = :ipaddress WHERE ...
?
【参考方案1】:
试试这个:
DELETE l, m
FROM visitors_list AS l
INNER JOIN visitors_main AS m ON l.information_ipaddress = m.information_ipaddress
WHERE l.information_ipaddress = :ipaddress
AND l.datetime_lastactive < NOW() - INTERVAL 3 HOUR;
【讨论】:
谢谢!但我只收到Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'information_ipaddress' in where clause is ambiguous' in ...
。为什么DELETE visitors_list
?我不想删除整个表。只是其中的数据(以及visitors_main)
@ErikEdgren - 这是因为两个表之间的外键约束,您也可以将 CASCADE DELETE
从另一个表中删除。但是您是要从两个表中删除还是只从其中一个表中删除?
我想从两个表中删除相同IP地址的数据。
@ErikEdgren - OK 抱歉,然后将连接条件更改为该列information_ipaddress
,然后将删除两个表中的所有匹配列。
谢谢,但我一直收到相同的错误消息 - Integrity constraint violation: 1052 Column 'information_ipaddress' in where clause is ambiguous'
:/ CASCADE DELETE visitors_list FROM visitors_list ...
也不起作用; Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASCADE DELETE visitors_list FROM visitors_list INNER JOIN visitors_main ON vi' at line 1'
【参考方案2】:
DELETE v1,v2
FROM visitors_list v1
INNER JOIN visitors_main v2 ON v1.id_visitor = v2.id
WHERE v1.information_ipaddress = :ipaddress
AND v1.datetime_lastactive < NOW() - INTERVAL 3 HOUR;
【讨论】:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'visitors_list.information_ipaddress' in 'where clause'' in ...
以上是关于使用 INNER JOIN 从两个表中删除数据的主要内容,如果未能解决你的问题,请参考以下文章
如何从另一个 SQL 表中获取两个不同列的匹配数据:Inner Join 和/或 Union?
MySQL 连接的使用:语法及案例剖析INNER JOINLEFT JOINRIGHT JOIN