本文实例讲述了PHP查询并删除数据库多列重复数据的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php $con = mysql_connect( "localhost" , "root" , "" ); if (! $con ) { die ( 'Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db( "test_db" , $con ); if (! $db_selected ) { die ( "Can/'t use test_db : " . mysql_error()); } $sql = "SELECT * FROM friend" ; $result =mysql_query( $sql , $con ); while ( $myrow =mysql_fetch_row( $result )) { $arr_data [ $myrow [0]]= $myrow [1]. "-" . $myrow [2]; } $arr_unique = array_unique ( $arr_data ); $arr_rep = array_diff_assoc ( $arr_data , $arr_unique ); //显示前后量 echo count ( $arr_data ). "-" . count ( $arr_unique ); foreach ( $arr_rep as $key => $value ){ $sql_del = "DELETE FROM friend WHERE id = '{$key}' " ; $result =mysql_query( $sql_del , $con ); } // 一些代码 mysql_close( $con ); ?> |
希望本文所述对大家PHP程序设计有所帮助。