Skip to content
PHP Logo

What does the USING statement do in PHP when deleting duplicates from a database table?

When deleting duplicates from a database table in PHP, the USING statement is used to specify the columns used for determining duplicates. By using the USING statement, you can ensure that only the specified columns are considered when identifying duplicates and deleting them from the table.

php
// Delete duplicates from a database table using the USING statement
$query = "DELETE t1 FROM table_name t1
          JOIN table_name t2 USING (column_name)
          WHERE t1.id > t2.id";
$result = mysqli_query($connection, $query);
if($result) {
    echo "Duplicates deleted successfully.";
} else {
    echo "Error deleting duplicates: " . mysqli_error($connection);
}