wp get_results - 过滤唯一记录 [英] wp get_results - filter unique records
本文介绍了wp get_results - 过滤唯一记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中有类别列,我必须使用 $wpdb->get_results
仅列出唯一类别,我该如何过滤它?见下面的代码...
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>
当前结果...
cat1
cat1
cat1
cat2
cat3
cat4
cat4
预期结果... cat1 cat2 cat3 cat4
推荐答案
使用 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);
这篇关于wp get_results - 过滤唯一记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文