1

結果の最大レコード数を 4 に制限するクエリによって、MySQL データベースにいくつかのレコードをクエリしていますが、この結果に、「keep_actual」列が 1 に設定されているが以前に作成されたすべてのレコードを除外するすべてのレコードを追加したいと考えています。これを行う最善の方法は何ですか?

  // Sample query which return those 4 i need
  // Result should be those 4 + all from current $cat which has keep_actual=1 but not are in those first 4
 $news = $ed->getByLanguage($lang_id, null, false, 4,false,true);

public function getByLanguage($lang, $cat=null, $main=true, $limit=null, $hasImage=false, $isTopped=false) {

        if ($cat == null) {
            $where = "language_id=$lang";
        } else {
            $where = "language_id=$lang and category_id=$cat";
        }
        if ($main == false && $isTopped==false) {
            $where.=" and main=0";
        }
        if ($hasImage == true) {
            $where.=" and main_picture is not null";
        } 

       if($cat==12){
        $limit=10;
       }

        //Nastavuje, zda se ma zobrazovat top nebo ne(na uvodni strane a aktualne)
        if($isTopped==true)
            $where.=" and top = 1";
        //=========================================================

        //$where.=" and entry.date >= '".date("Y-m-d h:i:s", mktime(0, 0, 0, 1, 1, date("Y")))."' and entry.date <= '".date("Y-m-d h:i:s")."'";
        if ($limit == 1) {
            //nahledy
          return parent::selectOne($this->sc->select("entry", "main_picture,url,name,abstract,date,top", $where, null, "date", $limit));
        } else {
            //main clanek
            return parent::selectAll($this->sc->select("entry", "*", $where, null, "date", $limit));
        }
    }


public function selectAll($sql) {
        $res = mysql_query($sql);
        if ($res) {
            $a = array();
            $i = 0;
            while ($r = mysql_fetch_assoc($res)) {
//                print_r($r);
                foreach ($r as $k => $v) {
                    $a[$i][$k] = $v;
                }
                $i++;
            }

            return $a;
        } else {
            return false;
        }
    }
4

2 に答える 2

1

さて、これは「ジェネリック」コードです。ニーズに合わせて調整する必要がありますが、これがどのように機能するかです。

$res = mysqli_query($sql, "SELECT * FROM `table` WHERE `Condition` = 1 LIMIT 4");
$results = array();
while($row = mysqli_fetch_array($res)) $results[] = $row;

$query = "SELECT * FROM `table` WHERE `keep_actual` = 1 AND ID NOT IN (";
for($i = 0; $i < 4; ++$i) $query .= "$row[ID], ";
$query .= "0)"; //To terminate the list of IDs
$res = mysqli_query($sql, $Query);

このコードはあなたが探しているものを与えるべきですが、それは完全にテストされておらず、かなり抽象的なものです。

于 2012-11-22T09:19:28.930 に答える
0

コメントを追加する担当者がいないため、これを回答として投稿する必要があります。

あなたが何をしようとしているのかを視覚化できるように、いくつかのサンプルデータを質問に入れることができれば本当に役に立ちます。いくつかの行だけを選択してから、クエリで選択されると予想される行を強調表示します。

于 2012-11-22T09:15:54.070 に答える