3

我需要为表“产品”生成一个 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

所有这些都在一个 SELECT 语句中。有可能还是我需要创建不同的语句然后连接结果?

4

4 回答 4

3

您可以在单个 SELECT 查询中执行此操作并动态构建它。

例如,假设您可以使用两个组合框在 1990 年到 2010 年之间拥有“模型”。组合框的默认值为“任意”:

$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 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 回答
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 回答
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 回答