2017-10-10 47 views
1

我在我的数据库中有类别栏,我只能使用$wpdb->get_results只能列出唯一类别我该如何过滤?看下面的代码...wp get_results - 过滤唯一记录

<?php 
global $wpdb; 
$table_name = $wpdb->prefix . 'servicer'; 
$results = $wpdb->get_results("SELECT * FROM " . $table_name . " WHERE is_deleted = 0", ARRAY_A); 
?> 
<ul class="list-unstyled"> 
<?php 

foreach($results as $key => $rec) 
{ 
    echo "<li>" . $rec['category'] . "</li>"; 
} 

?> 
</ul> 

当前结果...

cat1 
cat1 
cat1 
cat2 
cat3 
cat4 
cat4 

预期的结果... cat1 cat2 cat3 cat4

+1

'SELECT DISTINCT(id),* FROM ...'? –

+0

谢谢@JulienLachal –

回答

1

使用GROUP BY

$results = $wpdb->get_results("SELECT * FROM ".$table_name." WHERE 
is_deleted = 0 GROUP BY category", ARRAY_A); 

OR 使用DISTINCT

$results = $wpdb->get_results("SELECT DISTINCT(category),* FROM 
".$table_name." WHERE is_deleted = 0", ARRAY_A); 
+0

谢谢@Ankur Bhadania –

+0

非常欢迎。我很高兴它有帮助 –