muneeba9071
11-12-2012, 06:48 PM
I have a MySQL table with which stores LocationA, LocationB and distance. There are some duplicate entries, for example.
LocationA LocationB Distance
London Birmingham 180 miles
Birmingham London 180 miles.
As you can see, the second record is a duplicate because we already know the distance of London=>Birmingham using the first row. I have over 382,000 rows in total and these 382,000 can be reduced by 50% if the duplicates are removed.
I have tried the following script, but it stops in the middle for some reasons, and I want a MySQL query to do the job:
$results_locations = $wpdb->get_results("SELECT * FROM locations ");
foreach($results_locations as $location) {
$a_to_b = $wpdb->get_row("SELECT * FROM locations WHERE from_location = '{$location->from_location}' && to_location = '{$location->to_location}' ");
$b_to_a = $wpdb->get_row("SELECT * FROM locations WHERE from_location = '{$location->to_location}' && to_location = '{$location->from_location}' ");
if($a_to_b->id && $b_to_a->id) {
$wpdb->query("DELETE FROM locations WHERE id = '{$b_to_a->id}' ");
}
}
I would really appreciate if someone can help writing a MySQL query to fix this issue.
Many Thanks in advance!
LocationA LocationB Distance
London Birmingham 180 miles
Birmingham London 180 miles.
As you can see, the second record is a duplicate because we already know the distance of London=>Birmingham using the first row. I have over 382,000 rows in total and these 382,000 can be reduced by 50% if the duplicates are removed.
I have tried the following script, but it stops in the middle for some reasons, and I want a MySQL query to do the job:
$results_locations = $wpdb->get_results("SELECT * FROM locations ");
foreach($results_locations as $location) {
$a_to_b = $wpdb->get_row("SELECT * FROM locations WHERE from_location = '{$location->from_location}' && to_location = '{$location->to_location}' ");
$b_to_a = $wpdb->get_row("SELECT * FROM locations WHERE from_location = '{$location->to_location}' && to_location = '{$location->from_location}' ");
if($a_to_b->id && $b_to_a->id) {
$wpdb->query("DELETE FROM locations WHERE id = '{$b_to_a->id}' ");
}
}
I would really appreciate if someone can help writing a MySQL query to fix this issue.
Many Thanks in advance!