php - mysql DISTINCT GROUP QUERY 我如何列出组
问题描述
我有一个带有消息的 mysql 数据库,我试图在网页上显示用户和用户发送的消息数量,但是我只能设法将用户数量排除在查询之外,但经过大量调查后我发现我的查询是正确的,但我输出的方式不正确,但是我不知道如何同时显示用户名和消息量..
任何人都可以帮助我理解如何像这样正确输出结果..
messages
用户 1 (3) 用户 2 (6)
这是我目前的查询和输出。
我真的很感谢你们的任何帮助,因为这整天都在破坏我的大脑。
谢谢。
<div id='containercontact' class='containercontact'>
<?php
include_once('contactselect.php');
?>
</div>
-----contactselect.php----(variables set by another page with an ajax send)
if (isset($_GET['listID'])){$listID=$_GET['listID'];}
if (isset($_GET['itemaccountname'])){$itemaccountname=$_GET['itemaccountname'];}
if (isset($_GET['useraccountname'])){$useraccountname=$_GET['useraccountname'];}
if (isset($_GET['selecteduser'])){$selecteduser=$_GET['selecteduser'];} else {$selecteduser="nil";}
if ($useraccountname===$itemaccountname) {
echo"
<div class='userbox'>
<header class='ubheader'>Messages From</header>
<section class='ubmain'>
";
$db = new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$query="SELECT DISTINCT messageaccountname,count(*) FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname";
$stat=$db->prepare($query);
$stat->execute();
$Ccount = $stat->rowCount();
$contactcount=$Ccount;
if ($contactcount <1) {
echo"<div id='namecon' class='ubnames' >NO MESSAGES YET</div>";
}
else {
while($row = $stat->fetch()){
$messageaccountname=$row['messageaccountname'];
if ($messageaccountname != $useraccountname){
echo"<div id='namecon' class='ubnames' onclick='selectmessage(\"{$messageaccountname}\")'>{$messageaccountname}</div>";
}
}
}
echo "
</section>
</div>
";
}
?>
<script>
function selectmessage(selectauser){
var selected=selectauser;
var varLISTID = document.getElementById('datacatch').getAttribute("data-variable-LISTID");
var varUSERACCOUNTNAME = document.getElementById('datacatch').getAttribute("data-variable-USERACCOUNTNAME");
var varITEMACCOUNTNAME = document.getElementById('datacatch').getAttribute("data-variable-ITEMCCOUNTNAME");
var mylink = "loadmessages.php?listID=" + varLISTID + "&useraccountname=" + varUSERACCOUNTNAME + "&itemaccountname=" + varITEMACCOUNTNAME + "&selecteduser="+selected;
$('#infobox1').load(mylink);
}
</script>
我想我做到了
$db = new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$query="SELECT DISTINCT messageaccountname,count(*) FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname";
$stat=$db->prepare($query);
$stat->execute();
$Ccount = $stat->rowCount();
$contactcount=$Ccount;
if ($contactcount <1) {
echo"<div id='namecon' class='ubnames' >NO MESSAGES YET</div>";
}
else {
while($row = $stat->fetch()){
$messagenumber = $row['count(*)'];
$messageaccountname=$row['messageaccountname'];
if ($messageaccountname != $useraccountname){
echo"<div id='namecon' class='ubnames' onclick='selectmessage(\"{$messageaccountname}\")'>{$messageaccountname}{$messagenumber}</div>";
}
}
}
解决方案
推荐阅读
- javascript - jQuery 在表格元素中可选择和可滚动
- linux - 如何在特殊文件中记录我在 Vim 编辑器中使用的所有命令?
- r - 使用阈值标准聚合数据
- php - 如何在 PHP 中创建返回结果集的数组并计算每个组中不同类型的值
- swift - 获取连接的 AirPlay 设备名称
- javascript - javascript数组后退和前进按钮
- javascript - 如何将通过 xmlhttp.open 收集的变量传递给 PHP 源代码
- wordpress - whm 允许上传的最大文件大小:500 MB
- ruby-on-rails - 让 RubyMine 调试器与 Docker 一起工作的问题
- javascript - 如何在服务器端运行计时器