如何从php

时间:2015-12-21 11:51:16

标签: php mysql

假设我有一个包含这样的数据的数据库

id  userfrom userto body
1   a         b      hello
2   b         a      i am b
3   a         b      everyone
4   b         a      this is great

现在我想选择每个a和b中的1个或者你可以说我只想显示a和b的1个帖子所以应该是这个在php中的mysql查询和我的查询就像这样告诉我什么我必须接受更改以获取数据 抱歉写这么长的代码

<?php
                                $connection10=mysqli_connect('localhost','root','123456789','register');
        $selectquery="select * from messages where userfrom='$msg' or userto='$msg'";
        $resultquery=mysqli_query($connection10,$selectquery);
while($rowstand=mysqli_fetch_array($resultquery))
        {
            $userfrom1=$rowstand['userfrom'];
            $userto1=$rowstand['userto'];
            $body=$rowstand['msgbody'];
                $date=$rowstand['date'];
        $searchquery="select * from register where id='$userto1'";
            $searchresult=mysqli_query($connection10,$searchquery);
            while($bigrow=mysqli_fetch_array($searchresult))
            {
                $profileimage=$bigrow['profilepic'];
            $myfirstname=$bigrow['firstname'];
            $mylastname=$bigrow['lastname'];
            }
        echo '<a href="viewmessages.php?id='.$userto1.'"><img src="'.$profileimage.'" height="50" width="50"></a>'.$myfirstname.' '.$mylastname.'<br>'.$body.'<hr>';}
?>

3 个答案:

答案 0 :(得分:0)

select DISTINCT name,id,post from users

select DISTINCT name,id,post from users GROUP BY name

答案 1 :(得分:0)

您可以使用GROUP BY执行此操作,这将在您的示例中返回2个结果。使用GROUP_CONCAT连接属于同一名称的帖子可能会很好:

SELECT name, GROUP_CONCAT(post SEPARATOR ' ') 
FROM users
GROUP BY name;

输出:

  

a |大家好,

     

b |我是b这很棒

Fiddle

答案 2 :(得分:0)

您可以使用GROUP BY命令,如下所示: http://sqlfiddle.com/#!9/a0057/5/0