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?
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.