wp get_results - filter unique records

2019-05-31 04:05发布

i'm having category column in my database, i have to list only unique category by using $wpdb->get_results how can i filter it? see code below...

<?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>

current result...

cat1
cat1
cat1
cat2
cat3
cat4
cat4

expected result... cat1 cat2 cat3 cat4

标签: php wordpress
1条回答
一纸荒年 Trace。
2楼-- · 2019-05-31 05:06

Used GROUP BY

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

OR Used DISTINCT

$results  = $wpdb->get_results("SELECT DISTINCT(category),* FROM 
".$table_name." WHERE is_deleted = 0", ARRAY_A); 
查看更多
登录 后发表回答