0

準備ステートメントの日付に従って整理しようとしています。

DISTINCT 日付クエリ

$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>\n";
                    }

結果
2013-07-11
2013-07-12
2013-07-15

DISTINCT 日付クエリに基づくクエリ

$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>\n";
                    }

結果
680
681
682

私が探しているのは、次の形式の出力である必要があります。

2013-07-11
680
2013-07-12
681
2013-07-15
682
古い方法でこれは完了しましたが、prepare ステートメントが混乱しています...

4

1 に答える 1

0
 $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'];
                }

match_date で配列を作成しました。次の 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'];
                }

これで、$ids (内部に match_id を含む) と $dates (内部に match_date を含む) の 2 つの配列があります。そのため、好きな順に表示します。

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

これがお役に立てば幸いです。

于 2013-07-10T07:50:14.427 に答える