1

myMembers、forum_question、forum_answer の 3 つのテーブルがあります。マイメンバー:

id | usr ..|..| follow | ....
 1 | Bob . |..|3,2,....|.....
 2 | John  |..|1,8,5...|.....
 3 | ......|..|........The "follow" column store the data in an array of followers id.<p>

フォーラムの質問:

id | topic .| name  | idsender      | datetime
 1 | foo q .| Bob ..|.... 1........ | 07:20
 2 | foo1 q | Bob ..|.... 1........ | 07:10
 3 | foo2 q | John .|.... 2 ........| 06:00

フォーラムの回答:

question_id | answer | a_name | idsender | datetime
 1 .........| answ 1 | Bob ...| 1 .......| 08:20
 2 ....... .| answ 2 | Bob ...| 1 .......| 07:20
 1 .........| answ 3 | John ..| 2 .......| 08:00

私はこれを持っています:

$foll = mysql_query("SELECT follow FROM myMembers WHERE id='$id' LIMIT 1");
  while ($row = mysql_fetch_array($foll)){
    $foll1 = explode(",", $row["follow"]);
                      }
foreach ($foll1 as $key => $value){
$displ = mysql_query("SELECT p.* FROM
(SELECT id, datetime, topic, name, idsender, 'question' AS TYPE FROM 
forum_question WHERE idsender = '$value' UNION
SELECT question_id, datetime, answer, a_name, idsender, 'answer' AS TYPE FROM
forum_answer  WHERE idsender = '$value')p
ORDER BY p.datetime DESC");
        while ($row = mysql_fetch_array($displ)){
                $type= $row['TYPE'];
                $id = $row["id"];
                $topic = $row["topic"];
                $name = $row["a_name"];
                $idsender = $row["idsender"];
                $datetime=$row['datetime'];
echo '<div>'. $name .', '. $type .', '. $topic.', '. $datetime.'</div>'";

私はこの出力を持っています:

Bob, answer, answ1, 8:20
Bob, answer, answ2, 7:20
Bob, question, foo q, 7:20
Bob, question, foo1 q, 7:10
**John,** answer, answ 3, 8:00
**John,** question, foo2 q, 6:00

この出力が欲しいですか?

Bob, answer, answ1, 8:20
**John,** answer, answ3, 8:00
Bob, question, foo q, 7:20
Bob, answer, answ2, 7:20
Bob, question, foo1 q, 7:10
**John,** question, foo2 q, 6:00

可能です??ありがとう !

4

1 に答える 1

0

a_name に group by を使用し、datetime で並べ替えることができます。したがって、クエリは次のようになります。

$displ = mysql_query("SELECT p.* FROM
(SELECT id, datetime, topic, name, idsender, 'question' AS TYPE FROM 
forum_question WHERE idsender = '$value' UNION
SELECT question_id, datetime, answer, a_name, idsender, 'answer' AS TYPE FROM
forum_answer  WHERE idsender = '$value')p
GROUP BY p.a_name ORDER BY p.datetime DESC")
于 2013-04-10T06:40:13.317 に答える