我的数据库中有类别列,我必须使用 $wpdb->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
答案 0 :(得分:1)
使用GROUP BY
$results = $wpdb->get_results("SELECT * FROM ".$table_name." WHERE
is_deleted = 0 GROUP BY category", ARRAY_A);
或 使用DISTINCT
$results = $wpdb->get_results("SELECT DISTINCT(category),* FROM
".$table_name." WHERE is_deleted = 0", ARRAY_A);