drqyxkzbs21968684 2015-12-18 10:25
浏览 61
已采纳

加入两个while循环以根据日期顺序显示

So I have this MySQL query which is basically just two single while loops running and displaying info as they should, the issue with this is they display information like:

Test1, Test2, Test1, Test2, Test1, Test2, Test3, Test4, Test3, Test4, etc...

As you would expect. Although what I am looking to do is arrange these results based on the dates they were added to the table, so if one set of test 3 and test 4 were added most recently, it would show as:

test3, test4, test1, test2, test1, test2, test3, test4 etc...

So kind of rearranging these results based on the dates? I know using ORDER BY would only show each section in order but is there a way using my code which I can mix them ALL up and rearrange by both date_added and date_earned?

$user_id = $_SESSION['userid'];                 
$sql8 = "SELECT * FROM table1 ORDER BY date_added;";

if ($result8 = $conn->query($sql8)) {

    /* fetch associative array */
    while ($row8 = $result8->fetch_assoc()) {
        printf ("%s (%s)
", $row8["test1"], $row8["test2"]);
    }
}

$user_id = $_SESSION['userid'];                 
$sql9 = "SELECT * FROM table2 ORDER BY date_earned";

if ($result9 = $conn->query($sql9)) {

    /* fetch associative array */
    while ($row9 = $result9->fetch_assoc()) {
        printf ("%s (%s)
", $row9["test3"], $row9["test4"]);
    }
}    
  • 写回答

1条回答 默认 最新

  • duanpo1498 2015-12-18 10:35
    关注

    use union all

    Select * from(
    SELECT * FROM table1 
    union all
    SELECT * FROM table2 
    ) tab ORDER BY date_added,date_earned
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算