分组依据未显示所有行

时间:2015-04-19 12:54:36

标签: php mysql wordpress

我在WordPress工作,下面是我的选择查询。我使用了leftjoin和group by。但是如果我的articles.username coloumn中有重复的条目,则只返回一行。所以我希望所有行都以group by返回,并且在username字段中应该允许重复。 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

1 个答案:

答案 0 :(得分:1)

如果您想显示每个用户,那么我认为您应该由用户进行聚合。实际上,您应该聚合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";