[英]Select distinct in SQL?
The select distinct not working and the count(*) is not selecting the number of grouped rows but it is selecting the duplicate msg_contents.Please help.. I wanted to select distinct username and the number of duplicate usernames. select distinct 不起作用,count(*) 不是选择分组行数,而是选择重复的 msg_contents。请帮助.. 我想选择不同的用户名和重复用户名的数量。
<?php
require_once"cnc.php";
$sort = "SELECT hate_p FROM hate_t";
$qry = mysql_query($sort);
while($fet = mysql_fetch_assoc($qry)) {
if($fet == 0) {
echo "No Entries";
} else {
$sql = mysql_query("
SELECT DISTINCT
username,
msg_content,
COUNT(*) c
FROM messages
WHERE msg_content LIKE '%".$fet['hate_p']."%'
GROUP BY username HAVING c>0"
);
while($messages = mysql_fetch_assoc($sql)) {
?>
<tr>
<td><?php echo $messages['username'];?></td>
<td><?php echo $messages['c'];?></td>
</tr>
<?php
}
}
}
?>
you don't need distinct in this case, you can use group by only, distinct get all rows with distinct all columns you select (username,meg_content,count):在这种情况下,您不需要不同,您可以仅使用 group by,distinct 获取所有行与您选择的所有列都不同(用户名,meg_content,计数):
SELECT username,msg_content,COUNT(*) c
FROM messages WHERE msg_content LIKE '%".$fet['hate_p']."%'
GROUP BY username,msg_content
HAVING c>0
remove msg_content
from distinct?从不同的地方删除
msg_content
? you already have it.你已经拥有了。 and @gouda is right, you probably don't need the distinct at all.
@gouda 是对的,您可能根本不需要不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.