尝试删除左连接的行

时间:2016-01-13 18:07:49

标签: php

但我收到语法错误:

  

警告:PDOStatement :: execute():SQLSTATE [42000]:语法   错误或访问冲突:1064您的SQL语法有错误;   检查与您的MySQL服务器版本对应的手册   在'LEFT JOIN项目附近使用正确的语法                     

中的第2行上的projects_members.project_members_main_project = proj'
DELETE FROM projects_members
LEFT JOIN projects
    ON projects_members.project_members_main_project = projects.project_id
WHERE projects_members.project_members_id = ?
AND projects.project_page = ?

这是我的表格:

CREATE TABLE IF NOT EXISTS `projects_members` (
  `project_members_id` int(11) NOT NULL,
  `project_members_main_project` int(11) NOT NULL,
  `project_members_member` int(11) NOT NULL,
  `project_members_date` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `projects_members`
--

INSERT INTO `projects_members` (`project_members_id`, `project_members_main_project`, `project_members_member`, `project_members_date`) VALUES
(10, 1, 3, '2016-01-13 17:54:35');


CREATE TABLE IF NOT EXISTS `projects` (
  `project_id` int(11) NOT NULL,
  `project_name` varchar(100) NOT NULL,
  `project_page` int(11) NOT NULL,
  `project_page_type` varchar(15) NOT NULL,
  `project_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `project_startdate` datetime NOT NULL,
  `project_occupation` varchar(100) NOT NULL,
  `project_description` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `projects`
    --

    INSERT INTO `projects` (`project_id`, `project_name`, `project_page`, `project_page_type`, `project_date`, `project_startdate`, `project_occupation`, `project_description`) VALUES
    (1, 'kk', 1, 'company', '2015-11-18 14:50:45', '0000-00-00 00:00:00', 'bb', 'kkk'),
    (2, 'lol', 1, 'company', '2015-11-23 17:46:47', '2015-11-30 00:00:00', 'lol', 'fffff'),
    (3, 'lol', 1, 'company', '2015-11-23 17:48:07', '2015-11-30 00:00:00', 'lol', 'fffff'),
    (4, 'lol', 1, 'company', '2015-11-23 17:48:40', '2015-11-30 00:00:00', 'lol', 'fffff'),
    (5, 'lol', 1, 'company', '2015-11-23 17:49:35', '2015-11-30 00:00:00', 'lol', 'fffff'),
    (6, 'lol', 1, 'company', '2015-11-23 17:49:57', '2015-11-30 00:00:00', 'lol', 'fffff'),
    (7, 'lol', 1, 'company', '2015-11-23 17:51:06', '2015-11-30 00:00:00', 'lol', 'fffff'),
    (8, 'lol', 1, 'company', '2015-11-23 17:57:12', '2015-10-28 00:00:00', 'lol', 'oool');

1 个答案:

答案 0 :(得分:0)

您需要指定要删除的表。

仅从project_members表中删除:

DELETE project_members FROM projects_members
LEFT JOIN projects
    ON projects_members.project_members_main_project = projects.project_id
WHERE projects_members.project_members_id = ?
AND projects.project_page = ?

或仅从项目表中删除:

DELETE projects FROM projects_members
LEFT JOIN projects
    ON projects_members.project_members_main_project = projects.project_id
WHERE projects_members.project_members_id = ?
AND projects.project_page = ?

或从两个表中删除:

DELETE projects, project_members FROM projects_members
LEFT JOIN projects
    ON projects_members.project_members_main_project = projects.project_id
WHERE projects_members.project_members_id = ?
AND projects.project_page = ?