duanhuo3392 2013-12-22 09:54
浏览 547
已采纳

使用INNER JOIN从两个表中删除数据

I want to delete data from two tables with one SQL query according to datetime_lastactive and if the IP addresses is matching your own. But I'm getting this error message when I try out the following SQL query:

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

The tables looks like this:

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`)
)

How can I make this work?

  • 写回答

2条回答 默认 最新

  • donglu4633 2013-12-22 10:04
    关注

    Try this instead:

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding