douxingsuo8809 2013-07-10 07:27
浏览 33
已采纳

PHP Mysql根据日期准备查询排序

trying to sort out according with date in prepare statement.

DISTINCT date query

$stmt = $conn->prepare('SELECT DISTINCT match_date FROM premier_league
                    WHERE match_date >= :current_date
                    AND pre_selected = :pre_selected
                    ORDER BY match_date LIMIT 5');


                $stmt-> execute(array('current_date' => $current_date,
                        'pre_selected' => $pre_selected));

                $row_count_date = $stmt->rowCount();

                    $row_match_date = $stmt->fetchAll();

                    foreach ($row_match_date as $row) {

                        echo $row['match_date']."<br>
";
                    }

Result
2013-07-11
2013-07-12
2013-07-15

And query based on the DISTINCT date query

$stmt = $conn->prepare('SELECT match_id, LEFT (match_time, 5)  match_time, 
                    home_team, away_team, pre_selected, my_choice FROM premier_league
                    WHERE match_date >= :match_date
                    AND pre_selected = :pre_selected
                    ORDER BY match_time, home_team LIMIT 5');

                $stmt-> execute(array('match_date' => $current_date,
                        'pre_selected' => $pre_selected));

                $row_count_match = $stmt->rowCount();

                    $row_match = $stmt->fetchAll();

                    foreach ($row_match as $row) {

                        echo $row['match_id']."<br>
";
                    }

Result
680
681
682

what I am looking the output should be the following format.

2013-07-11
680
2013-07-12
681
2013-07-15
682
Old way I am done this but prepare statement confusing...

  • 写回答

1条回答 默认 最新

  • dongshang3309 2013-07-10 07:50
    关注
     $stmt = $conn->prepare('SELECT DISTINCT match_date FROM premier_league
                    WHERE match_date >= :current_date
                    AND pre_selected = :pre_selected
                    ORDER BY match_date LIMIT 5');
    
    
                $stmt-> execute(array('current_date' => $current_date,
                        'pre_selected' => $pre_selected));
    
                $row_count_date = $stmt->rowCount();
    
                    $row_match_date = $stmt->fetchAll();
    
                    foreach ($row_match_date as $row) {
    
                        $dates[] = $row['match_date'];
                    }
    

    I have create an array with match_date. Now I will do the same for the next match_id:

    $stmt = $conn->prepare('SELECT match_id, LEFT (match_time, 5)  match_time, 
                    home_team, away_team, pre_selected, my_choice FROM premier_league
                    WHERE match_date >= :match_date
                    AND pre_selected = :pre_selected
                    ORDER BY match_time, home_team LIMIT 5');
    
                $stmt-> execute(array('match_date' => $current_date,
                        'pre_selected' => $pre_selected));
    
                $row_count_match = $stmt->rowCount();
    
                    $row_match = $stmt->fetchAll();
    
                    foreach ($row_match as $row) {
    
                        $ids[] = $row['match_id'];
                    }
    

    Now, we have two arrays $ids (with match_id inside) and $dates (with match_date inside). So we will show in our like order.

          $i = 0;
          foreach($ids as $id) {
          echo $dates[$i];
          echo "<br/>";
          echo $id;
          echo "<br/>";
          $i++;   
          }
    

    I hope this will help you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?