Hi currently I started to use Medoo, when I checked documentation I couldn't find a way to delete records with inner join statement.
Is there any workaround to delete records with inner join using $db->delete()
method?
Original SQL Query:
DELETE t1 FROM customers t1 INNER JOIN customers t2 WHERE t1.id < t2.id AND (t1.customer_name = t2.customer_name AND t1.customer_unique_code = t2.customer_unique_code)
What I tried so far is using inner query like I did in $db->select
but unfortunately it didn't work.
Currently I am able to work it with $db->query()
but this is not an optimal solution for me.
$db->query(
'DELETE
t1 FROM '.$customers_db.' t1 INNER JOIN '.$customers_db.' t2 WHERE t1.id < t2.id AND(
t1.customer_name = t2.customer_name AND t1.customer_unique_code = t2.customer_unique_code)
');
I would like to know whether there is a better approach than this.
Thanks in advance for suggestions
You can do it in query but doing it with concatenation like that is not the best way to do it. Try using parametrisation like this:
$db->query(
'DELETE t1
FROM :custdb t1
INNER JOIN :custdb t2
WHERE t1.id < t2.id
AND (
t1.customer_name = t2.customer_name AND t1.customer_unique_code = t2.customer_unique_code
)',
[
":custdb" => "customer_DB_name"
]
);
I do not know how to do a join with a delete (I doubt it is possible) however, if you create the view you want in your database itself, then you could select the view in the delete statement without an issue.