-1

クエリがゼロ行を返す理由を見つけるのに苦労しています。私はこの質問でこれを広範囲に理解しようとしましたが、まともな結果は得られませんでした。

その要点は次のとおりです。

    $mydate=date("Y-m-d",strtotime("-3 months"));

    $foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate AND postdate > '$mydate' ORDER BY postdate DESC");
    $foo_query->execute( array('postdate' => $_REQUEST['postdate']) );

編集: このクエリは、日付を取得し、過去 3 か月に設定し、$mydate と呼ぶことになっています。次に、Postdate が $mydate より大きい BarTable からすべてのフィールドを取得し、クエリを実行します。

私が言ったことは、postdate が 3 か月前に等しく、かつ より大きい行を選択しているということです$mydate

私はこれをどのように言っているのか理解できません。:postdateは 3 か月前に等しくないためpostdate = :postdate、postdate が 3 か月前に等しい行を選択することはできません。

行を正しく表示するために、以前はWHERE postdate > '$mydate'.

postdate = :postdate AND postdate > '$mydate'パラメータを使用し、より大きいことに基づいてデータが選択されるように入力するにはどうすればよい$mydateですか?

4

4 に答える 4

1

パラメーター バインドを行いたい場合は、postdate が日付フィールドであると仮定して、これを試すことができます。

$foo_query=$DBH->prepare("
   SELECT * FROM BarTable 
   WHERE postdate = :postdate AND postdate > '$mydate' ORDER BY postdate DESC");
$foo_query->bindParam(':postdate', $_REQUEST['postdate'], PDO::PARAM_STR);
$foo_query->execute(); 

ただし、postdate がタイムスタンプの場合は、次のようになります。

$foo_query=$DBH->prepare("
   SELECT * FROM BarTable 
   WHERE postdate = :postdate AND postdate > '$mydate' ORDER BY postdate DESC");
$foo_query->bindParam(':postdate', $_REQUEST['postdate'], PDO::PARAM_INT);
$foo_query->execute(); 
于 2012-10-03T20:16:36.503 に答える
1

私があなたを正しく理解している場合(私にはまったくわかりません)、OR代わりに次のものが必要ですAND

$foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate OR postdate > '$mydate' ORDER BY postdate DESC");

このようにして、3 か月前より後の投稿と に投稿された投稿の両方を取得し:postdateます。

于 2012-10-03T19:51:01.307 に答える
1

のパラメータをバインドする必要がありますpostdate。また、両方の基準を満たすには、クエリでpostdate = :postdate and postdate > $mydateを使用する必要がありORます。

$mydate=date("Y-m-d",strtotime("-3 months"));
$foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate OR postdate > '$mydate' ORDER BY postdate DESC");
$foo->bindParam(':postdate', $_REQUEST['postdate'], PDO::PARAM_STR);
$foo_query->execute();

または、質問で述べたのと同様に、 を:割り当てるときにを逃しまし:postdate

 $mydate=date("Y-m-d",strtotime("-3 months"));
 $foo_query=$DBH->prepare("SELECT * FROM BarTable WHERE postdate = :postdate OR postdate   '$mydate' ORDER BY postdate DESC");
 $foo_query->execute( array(':postdate' => $_REQUEST['postdate']) );
于 2012-10-03T19:51:19.667 に答える
0

「BETWEEN」をお勧めします。

$foo_query=$DBH->prepare(
  "SELECT * FROM BarTable 
  WHERE postdate between ':somedate' AND '$mydate' 
  ORDER BY postdate DESC");
于 2012-10-03T19:54:16.283 に答える