2017-04-19 18 views
-1

私は価格で製品をフィルターしようとしていますが、特別価格で問題に遭遇しました。
以下の値よりも特別価格が製品に適用される場合、クエリはランダムな結果を示しています。価格でフィルターをかける

"SELECT * FROM tablename WHERE ((price >= ".(int)$min_price." AND price <= ".(int)$max_price." AND ('".date('Y-m-d')."' NOT BETWEEN special_price_startdate AND special_price_enddate OR special_price_startdate = NULL OR special_price_enddate= NULL)) OR (('".date('Y-m-d')."' BETWEEN special_price_startdate AND special_price_enddate AND special_price_startdate IS NOT NULL AND special_price_enddate IS NOT NULL) AND special_price >= ".(int)$min_price." AND special_price <= ".(int)$max_price.")) AND isactive = 1 AND isdeleted = 0 ORDER BY created DESC, productid DESC LIMIT ".(($page-1)*$perpage).",".$perpage; 
+1

あなたが期待している結果と実際に得られた結果を教えていただけたら助けが簡単になります! – RobFos

答えて

0

クエリは、その構文に従って行う必要があります。特別価格が適用され、特別価格が設定された限度の場合、行が返されます。 PHP入力のために調整

CREATE TABLE `tablename` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `price` int(11) DEFAULT NULL, 
    `special_price_startdate` date DEFAULT NULL, 
    `special_price_enddate` date DEFAULT NULL, 
    `isactive` bit(1) DEFAULT NULL, 
    `isdeleted` bit(1) DEFAULT NULL, 
    `special_price` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 

クエリ:

SELECT * 
    FROM tablename 
WHERE  ( ( price >= 99 
       AND price <= 999 
       AND ( '2017-04-21' NOT BETWEEN special_price_startdate 
              AND special_price_enddate 
        OR special_price_startdate = NULL 
        OR special_price_enddate = NULL)) 
      OR ( ( '2017-04-21' BETWEEN special_price_startdate 
              AND special_price_enddate 
        AND special_price_startdate IS NOT NULL 
        AND special_price_enddate IS NOT NULL) 
       AND special_price >= 99 
       AND special_price <= 999)) 
     AND isactive = 1 
     AND isdeleted = 0 
/*ORDER BY created DESC, productid DESC*/ 
LIMIT 10; 

挿入シードデータ:

INSERT INTO `tablename` values 
(1, 120, '2017-04-20', '2017-04-22', 1, 0, 125), 
(2, 125, '2017-04-27', '2017-04-28', 1, 0, 125), 
(3, 120, '2017-04-20', '2017-04-22', 1, 0, 50), 
(4, 50, '2017-04-27', '2017-04-28', 1, 0, 125) 
特別価格が適用されず、通常価格が設定された限界値の間にある場合、それ以外の場合は、行を返します

出力:

1 120 4/20/2017 12:00:00 AM 4/22/2017 12:00:00 AM 1 0 125 
2 125 4/27/2017 12:00:00 AM 4/28/2017 12:00:00 AM 1 0 125 
関連する問題