在sql中选择查询

时间:2012-08-19 20:08:05

标签: php mysql

我需要为表格'生成一个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 个答案:

答案 0 :(得分:3)

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

例如,假设您可以使用两个组合框在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)";
}

现在您有一系列附加的,可选的条件,可以与ANDs结合使用:

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

如果不是空的,可以使用结果条件:

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

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

您还可以使用与$ betweens中列出的字段不同的字段添加“等于”条件。

答案 1 :(得分: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]

答案 2 :(得分: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'));

答案 3 :(得分: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

中时才可能