SQL查询连接顺序依据

时间:2016-08-12 22:02:38

标签: php mysql sql database join

我制作和Instagram模仿,我需要做一个饲料。我有2张桌子"关注"和"帖子"。

这是"以下"数据库:

following database

这是"帖子"数据库:

Posts database

以下是我的代码,可以尝试选择用户关注的人并将他们的帖子放入他们的Feed:

<?php

$username = $_SESSION['username'];
$sql = "SELECT username_follow FROM following WHERE follower = '$username'";
$rsu = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($rsu)){
  $uername2 = $row[0];
  $sql2 = "SELECT pic, `time` FROM `Posts` WHERE username = '$uername2' ORDER BY `Posts`.`id` DESC";
  $res = mysql_query($sql2, $conn);
  //echo $row[0];
  while ($row2 = mysql_fetch_row($res)) {
    echo "<br> <br>";
    echo "<center>";
    echo "<img width='450px' height='450px' src='data:image;base64,".base64_encode($row2[0])."'>";

    echo "<br> <br>";
    echo "</center>";
  }
}

?>

但是如何按时间顺序显示图片?

1 个答案:

答案 0 :(得分:1)

加入两个表并按my $parsed_time = str2time($stime); if (!defined $parsed_time) { # ... handle this case } else { my $dur = $etime - $parsed_time; ... }

排序
time