错误号:1064(使用 sql JOIN 进行删除)
我想使用 sql JOIN 删除两个数据库表(mysql)中的 ROW,但出现错误:
这是我的 sql 代码:
table_1.id =>有一行 ID 为
3
table_2.rela =>具有真实3
的三行(有时与 table_1.id 连接的数字行 table_2.rela 是 1 或 5 或 2 或 ...)
//$id = $this->input->post('id');
$id = '3';
$this->db->query("
DELETE *
FROM table_1
JOIN table_2
ON table_1.id = table_2.rela
WHERE table_1.id = '.$id.'");
通过上面的查询,我收到此错误:
发生数据库错误
错误号:1064您的 SQL 语法有错误;检查手册 与您的 MySQL 服务器版本相对应,以便使用正确的语法 靠近“* FROM table_1 JOIN table_2 ON table_1”。在第 1 行
DELETE * FROM table_1 JOIN table_2 ON table_1.id = table_2.rela WHERE table_1.id = '.3.'
如何修复它?
I want delete ROW(s) in two database table(mysql) with sql JOIN but get error:
This is my sql code:
table_1.id => have one row with id
3
table_2.rela => have three row with real3
(sometimes number row table_2.rela join with table_1.id is 1 or 5 or 2 or ...)
//$id = $this->input->post('id');
$id = '3';
$this->db->query("
DELETE *
FROM table_1
JOIN table_2
ON table_1.id = table_2.rela
WHERE table_1.id = '.$id.'");
With above query i get this error:
A Database Error Occurred
Error Number: 1064You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '* FROM table_1 JOIN table_2 ON table_1.' at line 1DELETE * FROM table_1 JOIN table_2 ON table_1.id = table_2.rela WHERE
table_1.id = '.3.'
How is fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DELETE
之后不能使用通配符。您必须指定要从中删除记录的表。您的查询应该类似于:假设 ids 是 int。如果 $id 可能来自用户输入,这也会删除无效字符。
You cannot use the wildcard after the
DELETE
. You have to specify the table from which you want to delete records. Your query should be something like:Assuming that the ids are int. This also removes invalid characters if the $id may come from user input.
您应该在
FROM
子句之前至少提及一个要从中删除匹配行的表名称。如果要从两个表中删除匹配的行,请提及以逗号分隔的两个表。You should mention at least one table name before the
FROM
clause, from which you wants to delete the matching rows . If you want to delete the matching rows from both tables, mention both of the tables separated by comma.另外,在您原来的问题中,您使用的字符串错误,您 $id 用双引号括起来,并且您使用 single 来尝试连接...因为它在双引号中,并且您的 $id 很可能是您不需要的数字将其用单引号括起来以用于 sql...您所需要的只是
"table_1.id = $id";
Also in your original question you are using the string wrong you $id is wrapped by double quotes and you use single to try and concatenate... since its in double quotes and your $id is most likely a number you don't need to wrap it in single quotes for the sql... all you would need is
"table_1.id = $id";