-1

现在我正在使用这个查询,但它向我显示了重复和不重复的列,我只想获得不重复的列,

<?

$queryy=mysql_query("SELECT   
 mybb_posts.pid, mybb_posts.subject,mybb_posts.message,mybb_posts.tid,
mybb_attachments.pid,mybb_attachments.aid
FROM mybb_posts
INNER JOIN mybb_attachments
USING(pid,pid)
ORDER  BY  `aid` DESC
LIMIT 7")or die($query."<br/><br/>".mysql_error());

while ($lista=mysql_fetch_array($queryy))
{
echo '


            <img src="attachment.php?aid='.$lista["aid"].'   " alt="" />
                <h4><a href="#" >'.$lista["subject"].'</a></h4>

            ';

}

?>
4

1 回答 1

0

尝试在您的选择中使用 DISTINCT。

SELECT DISTINCT
posts.pid, 
posts.subject,
posts.message,
posts.tid, 
attach.pid,
attach.aid 
FROM mybb_posts post
INNER JOIN mybb_attachments attach
ON mybb_posts.pid = mybb_attachments.pid
ORDER  BY  `aid` DESC LIMIT 7

此 DISTINCT 查询应仅返回 PID 在两列中匹配的唯一结果。如果有多个实体与该 PID 相关,它将插入另一行。

只有在对实体使用聚合函数时才能使用 group by。例如: SELECT 中的 SUM(attach.aid) 将允许您按 PID 分组

如果您要为一条记录返回一堆不同的整数,并且不想要重复的行,您可以使用 group by 将结果汇总到一行中......

这是描述我的散文的参考。

http://blog.sqlauthority.com/2007/03/29/sql-server-difference-between-distinct-and-group-by-distinct-vs-group-by/

于 2012-05-29T16:35:45.733 回答