如何通过PHP使用Distinct函数显示唯一值
如何通过PHP仅显示不同的值-以便结果输出是唯一的 distinct函数对此行没有任何影响如何通过PHP使用Distinct函数显示唯一值,php,mysql,Php,Mysql,如何通过PHP仅显示不同的值-以便结果输出是唯一的 distinct函数对此行没有任何影响 #__new_categories.cat_name as cat_name 这是完整的代码 <?php $db = JFactory::getDBO(); $query = $db->getQuery(true); $c = $item->prod_cat_id; $query = "SELECT DISTINCT #__new_cat
#__new_categories.cat_name as cat_name
这是完整的代码
<?php
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$c = $item->prod_cat_id;
$query = "SELECT
DISTINCT #__new_categories.cat_name as cat_name
, #__new_categories.cat_parent as cat_parent
, #__new_products.prod_name as prod_name
from #__new_categories
inner join #__new_products on #__new_products.prod_cat_id = #__new_categories.id
where #__new_products.prod_cat_id = $c";
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $row){
echo $row->cat_name;
}
?>
问题来自
内部联接
您可以对查询执行以下操作:
$results = $db->loadObjectList();
$uniqueValues = array();
foreach($results as $row){
if (!isset($uniqueValues[$row->cat_name])) {
echo $row->cat_name;
$uniqueValues[$row->cat_name] = true;
}
}
按新类别分组。类别名称
或者使用PHP处理此问题,因为这很可能会破坏您的查询:
$results = $db->loadObjectList();
$uniqueValues = array();
foreach($results as $row){
if (!isset($uniqueValues[$row->cat_name])) {
echo $row->cat_name;
$uniqueValues[$row->cat_name] = true;
}
}
为什么不使用mysql中的distinct函数?为什么不在sql查询中使用distinct函数?@VasilShaddix,ac.freeloper-问题编辑。Pl参见中使用的distinct。它没有impact@Ruchika为什么使用内部联接?这就是您的问题。由于内部联接,是否不可能获得唯一的输出?