错误号:1064(使用 sql JOIN 进行删除)

发布于 2024-12-11 08:19:25 字数 861 浏览 0 评论 0原文

我想使用 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 real 3 (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: 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 '* FROM table_1 JOIN table_2 ON table_1.' at line 1

DELETE * FROM table_1 JOIN table_2 ON table_1.id = table_2.rela WHERE
table_1.id = '.3.'

How is fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在风中等你 2024-12-18 08:19:25

DELETE 之后不能使用通配符。您必须指定要从中删除记录的表。您的查询应该类似于:

$this->db->query("
        DELETE table_1, table_2
        FROM table_1
        JOIN  table_2
           ON table_1.id = table_2.rela
        WHERE table_1.id = ".floor($id));

假设 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:

$this->db->query("
        DELETE table_1, table_2
        FROM table_1
        JOIN  table_2
           ON table_1.id = table_2.rela
        WHERE table_1.id = ".floor($id));

Assuming that the ids are int. This also removes invalid characters if the $id may come from user input.

浅浅 2024-12-18 08:19:25

您应该在 FROM 子句之前至少提及一个要从中删除匹配行的表名称。如果要从两个表中删除匹配的行,请提及以逗号分隔的两个表。

$this->db->query("
        DELETE table_1
        FROM table_1
        JOIN  table_2
           ON table_1.id = table_2.rela
        WHERE table_1.id = '".$id."'");

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.

$this->db->query("
        DELETE table_1
        FROM table_1
        JOIN  table_2
           ON table_1.id = table_2.rela
        WHERE table_1.id = '".$id."'");
软糯酥胸 2024-12-18 08:19:25
$id = '3';
    $this->db->query("
    DELETE table_1, table_2
    FROM table_1
    JOIN  table_2
       ON table_1.id = table_2.rela
    WHERE table_1.id = $id");

另外,在您原来的问题中,您使用的字符串错误,您 $id 用双引号括起来,并且您使用 single 来尝试连接...因为它在双引号中,并且您的 $id 很可能是您不需要的数字将其用单引号括起来以用于 sql...您所需要的只是 "table_1.id = $id";

$id = '3';
    $this->db->query("
    DELETE table_1, table_2
    FROM table_1
    JOIN  table_2
       ON table_1.id = table_2.rela
    WHERE 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";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文