简体   繁体   English

分组依据未显示所有行

[英]Group by is not showing all the rows

I am working in WordPress and below is my select query. 我正在WordPress中工作,下面是我的选择查询。 I have used leftjoin and group by. 我用过leftjoin和group by。 But only one row is returned if I have duplicate entries in my articles.username coloumn. 但是,如果我的articles.username coloumn中有重复的条目,则仅返回一行。 So I want all the rows to be returned with group by and duplicates should be allowed in username field. 因此,我希望所有行都与group by一起返回,并且应在用户名字段中允许重复。 PHP Code 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());

Snapshot of articles table 文章快照表 这是文章表

Snapshot of votes table (currently no data in it) 投票表快照(当前无数据) 投票表

Below is my full code 下面是我的完整代码

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

I have a drop down on the top of the page and a search button as shown below on pressing search the results are shown but if I add duplicate values in username field of articles table I get only 1 row in result. 我在页面顶部有一个下拉菜单,并在按下搜索按钮时显示如下所示的搜索按钮,但显示的是结果,但是如果我在articles表的username字段中添加重复值,则结果将只有1行。 My page 我的页面 在此处输入图片说明

If you want to show each user, then I think you should be aggregating by the user. 如果要显示每个用户,那么我认为您应该按用户进行汇总。 In fact, you should be aggregating by every column in the SELECT that is not an argument to an aggregation function. 实际上,您应该按SELECT中不是聚合函数参数的每一列进行聚合。

This may do what you want: 这可能会做您想要的:

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM