3

テーブル 'products' の SQL クエリを生成する必要があります。テーブルの構造は次のとおりです。

item_no   pack_no   name   model   sellingprice   discount   price_rs    quality

 101       1001      aa     2001      $500          10%      Rs.24750   excellent
 102       1002      bb     1996      $400           5%      Rs.20900     poor
 103       1003      cx     1986      $400           5%      Rs.20900     good
 104       1004      dx     2010      $500          10%      Rs.24750     poor
 .           .        .      .         .             .            .
 .           .        .      .         .             .            .


 .           .        .      .         .             .            . 
 .           .        .      .         .             .            .

 500       5000      bx     1998      $200           10%     Rs.9900      very good

等々..

次のようなさまざまな条件があります。

言う、

すべての製品をフィルタリングする必要があります

model is between 1990 to 2010   
AND
sellingprice is between 600 to 700  
AND
discount is between 0 to 20%
AND 
quality is between good and excellent

これらすべてを 1 つの SELECT ステートメントで行います。それは可能ですか、それとも別のステートメントを作成してから結果を連結する必要がありますか?

4

4 に答える 4

3

単一の SELECT クエリでそれを実行し、動的に構築できます。

たとえば、2 つのコンボボックスを使用して、1990 年から 2010 年までの「モデル」を使用できるとします。コンボボックスのデフォルトは「any」です:

$ands = array();

$betweens = array('model', 'sellingprice', ...);

foreach ($betweens as $basekey)
{
    $key1 = $basekey . '_from';
    if (!isset($_POST[$key1]))
        continue;
    if ('' == ($val1 = $_POST[$key1]))
        continue;
    $key2 = $basekey . '_to';
    if (!isset($_POST[$key2]))
        continue;
    if ('' == ($val2 = $_POST[$key2]))
        continue;
    // Check that val1 and val2 have sane values
    // PDO would be a little different, and slightly more complicated

    // On the other hand, if NOT using PDO, SQL injection has to be taken into
    // account. Just in case.
    if (!is_numeric($val1))
        $val1 = "'" . mysql_real_escape_string($val1) . "'";
    if (!is_numeric($val2))
        $val2 = "'" . mysql_real_escape_string($val2) . "'";

    $ands[] = "($basekey BETWEEN $val1 AND $val2)";
}

これで、AND で結合できる追加のオプションの条件の配列ができました。

$and_query = implode(' AND ', $ands);

結果の条件が空でない場合は、次のように使用できます。

$query = "SELECT ... WHERE ( main conditions )";

if (count($ands))
    $query .= " AND ( $and_query ) ";

$ betweens にリストされているものとは異なるフィールドで「等しい」条件を追加することもできます。

于 2012-08-19T20:25:17.273 に答える
2

はい、可能です。

Select * from table
where
(model between 1990 and 2010)  
AND
(sellingprice between 600 and 700)  
AND
(discount between 0 and 20)
AND 
(quality IN ('good', 'very good', 'excellent'));
于 2012-08-19T20:20:16.407 に答える
2

1つの選択クエリで実行できます....サンプルは次のとおりです。

select field-1, field-2,.....,field-n
from tablename
where (field-1 between value-1 and value-2)
AND (field-2 between value-3 and value-4)
AND (field-3 IN ('value-5', 'value-6',...,'value-n'))
............
AND (field-n ....[CONDITION].......)
where [CONDITION]
于 2012-08-19T20:12:45.477 に答える
1
SELECT * 
FROM products 
WHERE model >=1990 AND model <= 2010 
    AND sellingprice >= 600 AND sellingprice <=700 
    AND discount <= 20 
    AND (quality ="good" OR quality ="very good" OR quality ="excellent")

「$」および「%」記号が DB に格納されていない場合にのみ可能

于 2012-08-19T20:18:31.293 に答える