php - 分组依据未显示所有行

标签 php mysql wordpress

我在 WordPress 工作,下面是我的选择查询。我用过 leftjoin 和 group by。但是如果我的 articles.username 列中有重复的条目,则只返回一行。所以我希望所有的行都与 group by 一起返回,并且用户名字段中应该允许重复。 PHP代码

$sqll = "SELECT articles.aid, articles.username, articles.competition, articles.path, articles.category, articles.title, Sum(zvotes.zvotes) AS votessum FROM articles LEFT JOIN zvotes on articles.aid=zvotes.aid GROUP BY articles.competition HAVING articles.category = '$cat' && articles.competition = '$comp' ORDER BY votessum";

    $results = $wpdb->get_results($wpdb->prepare($sqll)) or die(mysql_error());

文章表的快照 This is the articles table

投票表的快照(目前没有数据) Votes table

下面是我的完整代码

echo '<form action="" method="post">';
echo '<select name="category" id="category" style="width:250px; background-color:lightgrey;">';
echo    '<option value="" disabled="disabled" selected="selected" ">Select category</option>';
echo   '<option value="My Testimony">My Testimony</option>';
echo    '<option value="Love & Relationships">Love & Relationships</option>';
echo    '<option value="Miscellaneous">Miscellaneous</option>';
echo '</select>'; 
echo    '<input type="submit" name="a" value="Search"  style="margin-left:15px; margin-bottom:15px;">';
echo '</form>';


//show after drop down value is selected
if(isset($_POST['a'])){
//echo "zeeshanaslamdurrani". "<br>";

echo do_shortcode('[ujicountdown id="Photos Contest" expire="2015/04/30 00:00" hide="true" url="" subscr="sdf" recurring="" rectype="second" repeats=""]');
      global $wpdb;
//get current competition value
$cat =$_POST['category'];
        $comp = $wpdb->get_var("SELECT competition FROM competition ORDER BY cid DESC LIMIT 1");
//echo $comp;
        $sql = "SELECT * FROM articles WHERE category='$cat'";

      $comp = $wpdb->get_var("SELECT competition FROM competition ORDER BY cid DESC LIMIT 1");
            echo "current competition is ". $comp;

//test query
      $sqll = "SELECT articles.aid, articles.username, articles.competition, articles.path, articles.category, articles.title, Sum(zvotes.zvotes) AS votessum FROM articles LEFT JOIN zvotes on articles.aid=zvotes.aid GROUP BY articles.competition HAVING articles.category = '$cat' && articles.competition = '$comp' ORDER BY votessum";

$results = $wpdb->get_results($wpdb->prepare($sqll)) or die(mysql_error());

foreach( $results as $result ) {
echo '<form action="" method="post">';
echo "<input name='category' type='hidden' value='$result->category'>";
echo $result->title.'<br>';
echo "<img src='$result->path' width='150' height='150' >" . '<br><br>';
echo $result->body.'<br>';
echo "<input name='comp' type='hidden' value='$result->competition'>";
echo $result->username.'<br>';

echo $result->votessum.'<br>';
echo "<input style='margin-bottom:30px;' value='vote' name='submit' type='submit'/></form>";    

}//end of foreach




}//end of isset

我在页面顶部有一个下拉菜单和一个搜索按钮,如下所示,按下搜索时会显示结果,但如果我在文章表的用户名字段中添加重复值,我只会得到 1 行结果。 我的页面enter image description here

最佳答案

如果你想显示每个用户,那么我认为你应该按用户聚合。事实上,您应该按 SELECT 中不是聚合函数参数的每一列进行聚合。

这可能会做你想做的事:

SELECT a.aid, a.username, a.competition, a.path, a.category, a.title,   
       Sum(z.zvotes) AS votessum
FROM articles a LEFT JOIN
     zvotes z
     on a.aid = z.aid
WHERE a.category = '$cat' AND a.competition = '$comp'
GROUP BY a.aid, a.username, a.competition, a.path, a.category, a.title
ORDER BY votessum";

关于php - 分组依据未显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29730122/

相关文章:

mysql - MySQL 中何时使用单引号、双引号和反引号

php - 警告 : sizeof(): Parameter must be an array or an object that implements Countable

PHP,禁用自动输入[]到数组

PHP 表单提交时自动递增数字

php - Zend Framework 2 控制台路由

python - Django 是否对 Unicode (utf-8?) 字符串进行双重编码?

带有两个时间戳的 PHP If 语句

mysql - 在 MySQL 中使用 COUNT 时如何返回 0 而不是 null

css - 某些类不再有效但显示在 css 和 html 中

php - 如何从 WP_Post 对象获取标题