-2

我想在 MySQL 中使用具有两个条件的SELECT语句。AND这是我的表和插入,然后是 select 语句。我没有从 select 语句中得到任何结果。

-- 
-- Table structure for table `indextable`
-- 

CREATE TABLE `indextable` (
  `post_id` bigint(40) NOT NULL,
  `key` varchar(20) NOT NULL,
  `value` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL,
  KEY `key_2` (`key`),
  KEY `value_2` (`value`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table `indextable`
-- 

INSERT INTO `indextable` VALUES (390, 'doors', '360', 398);
INSERT INTO `indextable` VALUES (390, 'body_type', '347', 398);
INSERT INTO `indextable` VALUES (390, 'seller_type', '11', 398);
INSERT INTO `indextable` VALUES (390, 'mechanical_condition', '6', 398);
INSERT INTO `indextable` VALUES (390, 'body_condition', '1', 398);
INSERT INTO `indextable` VALUES (390, 'kilometers', '', 398);
INSERT INTO `indextable` VALUES (390, 'year', '2012', 398);
INSERT INTO `indextable` VALUES (390, 'mobile', '', 398);
INSERT INTO `indextable` VALUES (390, 'price', '111111111111', 398);
INSERT INTO `indextable` VALUES (388, 'city', 'localhost', 398);
INSERT INTO `indextable` VALUES (388, 'description', '<!--:en-->dddddddddd', 398);
INSERT INTO `indextable` VALUES (388, 'title', '<!--:en-->555       ', 398);
INSERT INTO `indextable` VALUES (388, 'categories_name', 'Cars souq', 398);
INSERT INTO `indextable` VALUES (388, 'images', 'http://localhost/wor', 398);
INSERT INTO `indextable` VALUES (388, 'fuel_type', '380', 398);
INSERT INTO `indextable` VALUES (388, 'warranty', '725', 398);
INSERT INTO `indextable` VALUES (388, 'horsepower', '387', 398);
INSERT INTO `indextable` VALUES (388, 'transmission_type', '377', 398);
INSERT INTO `indextable` VALUES (388, 'color', '707', 398);
INSERT INTO `indextable` VALUES (388, 'no_of_cylinders', '365', 398);
INSERT INTO `indextable` VALUES (388, 'doors', '360', 398);
INSERT INTO `indextable` VALUES (388, 'body_type', '347', 398);
INSERT INTO `indextable` VALUES (388, 'seller_type', '11', 398);
INSERT INTO `indextable` VALUES (388, 'mechanical_condition', '6', 398);
INSERT INTO `indextable` VALUES (388, 'body_condition', '1', 398);
INSERT INTO `indextable` VALUES (388, 'kilometers', '', 398);
INSERT INTO `indextable` VALUES (388, 'year', '2012', 398);
INSERT INTO `indextable` VALUES (388, 'mobile', '', 398);
INSERT INTO `indextable` VALUES (388, 'price', '3333', 398);
INSERT INTO `indextable` VALUES (390, 'no_of_cylinders', '365', 398);
INSERT INTO `indextable` VALUES (390, 'color', '707', 398);
INSERT INTO `indextable` VALUES (390, 'transmission_type', '377', 398);
INSERT INTO `indextable` VALUES (390, 'horsepower', '387', 398);
INSERT INTO `indextable` VALUES (390, 'warranty', '725', 398);
INSERT INTO `indextable` VALUES (390, 'fuel_type', '380', 398);
INSERT INTO `indextable` VALUES (390, 'images', '', 398);
INSERT INTO `indextable` VALUES (390, 'categories_name', 'Cars souq', 398);
INSERT INTO `indextable` VALUES (390, 'title', '<!--:en-->ddddddddd<', 398);
INSERT INTO `indextable` VALUES (390, 'description', '<!--:en-->dddddddddd', 398);
INSERT INTO `indextable` VALUES (390, 'city', 'localhost', 398);
INSERT INTO `indextable` VALUES (392, 'price', '133', 398);
INSERT INTO `indextable` VALUES (392, 'mobile', '', 398);
INSERT INTO `indextable` VALUES (392, 'year', '2012', 398);
INSERT INTO `indextable` VALUES (392, 'kilometers', '222', 398);
INSERT INTO `indextable` VALUES (392, 'body_condition', '1', 398);
INSERT INTO `indextable` VALUES (392, 'mechanical_condition', '6', 398);
INSERT INTO `indextable` VALUES (392, 'seller_type', '11', 398);
INSERT INTO `indextable` VALUES (392, 'body_type', '347', 398);
INSERT INTO `indextable` VALUES (392, 'doors', '360', 398);
INSERT INTO `indextable` VALUES (392, 'no_of_cylinders', '365', 398);
INSERT INTO `indextable` VALUES (392, 'color', '707', 398);
INSERT INTO `indextable` VALUES (392, 'transmission_type', '377', 398);
INSERT INTO `indextable` VALUES (392, 'horsepower', '387', 398);
INSERT INTO `indextable` VALUES (392, 'warranty', '725', 398);
INSERT INTO `indextable` VALUES (392, 'fuel_type', '380', 398);
INSERT INTO `indextable` VALUES (392, 'images', '', 398);
INSERT INTO `indextable` VALUES (392, 'categories_name', 'Cars souq', 398);
INSERT INTO `indextable` VALUES (392, 'title', '<!--:en-->ddddddddd<', 398);
INSERT INTO `indextable` VALUES (392, 'description', '<!--:en-->dddddddddd', 398);
INSERT INTO `indextable` VALUES (392, 'city', 'localhost', 398);
INSERT INTO `indextable` VALUES (393, 'price', '111111111111', 398);
INSERT INTO `indextable` VALUES (393, 'mobile', '', 398);
INSERT INTO `indextable` VALUES (393, 'year', '2012', 398);
INSERT INTO `indextable` VALUES (393, 'kilometers', '', 398);
INSERT INTO `indextable` VALUES (393, 'body_condition', '1', 398);
INSERT INTO `indextable` VALUES (393, 'mechanical_condition', '6', 398);
INSERT INTO `indextable` VALUES (393, 'seller_type', '11', 398);
INSERT INTO `indextable` VALUES (393, 'body_type', '347', 398);
INSERT INTO `indextable` VALUES (393, 'doors', '360', 398);
INSERT INTO `indextable` VALUES (393, 'no_of_cylinders', '365', 398);
INSERT INTO `indextable` VALUES (393, 'color', '707', 398);
INSERT INTO `indextable` VALUES (393, 'transmission_type', '377', 398);
INSERT INTO `indextable` VALUES (393, 'horsepower', '387', 398);
INSERT INTO `indextable` VALUES (393, 'warranty', '725', 398);
INSERT INTO `indextable` VALUES (393, 'fuel_type', '380', 398);
INSERT INTO `indextable` VALUES (393, 'images', '', 398);
INSERT INTO `indextable` VALUES (393, 'categories_name', 'Cars souq', 398);
INSERT INTO `indextable` VALUES (393, 'title', '<!--:en-->ddddddddd<', 398);
INSERT INTO `indextable` VALUES (393, 'description', '<!--:en-->dddddddddd', 398);
INSERT INTO `indextable` VALUES (393, 'city', 'localhost', 398);
INSERT INTO `indextable` VALUES (398, 'price', '11333', 398);
INSERT INTO `indextable` VALUES (398, 'mobile', '', 398);
INSERT INTO `indextable` VALUES (398, 'year', '2012', 398);
INSERT INTO `indextable` VALUES (398, 'kilometers', '', 398);
INSERT INTO `indextable` VALUES (398, 'body_condition', '1', 398);
INSERT INTO `indextable` VALUES (398, 'mechanical_condition', '6', 398);
INSERT INTO `indextable` VALUES (398, 'seller_type', '11', 398);
INSERT INTO `indextable` VALUES (398, 'body_type', '347', 398);
INSERT INTO `indextable` VALUES (398, 'doors', '360', 398);
INSERT INTO `indextable` VALUES (398, 'no_of_cylinders', '365', 398);
INSERT INTO `indextable` VALUES (398, 'color', '707', 398);
INSERT INTO `indextable` VALUES (398, 'transmission_type', '377', 398);
INSERT INTO `indextable` VALUES (398, 'horsepower', '387', 398);
INSERT INTO `indextable` VALUES (398, 'warranty', '725', 398);
INSERT INTO `indextable` VALUES (398, 'fuel_type', '380', 398);
INSERT INTO `indextable` VALUES (398, 'images', 'http://localhost/wor', 398);
INSERT INTO `indextable` VALUES (398, 'categories_name', 'Cars souq', 398);
INSERT INTO `indextable` VALUES (398, 'title', '<!--:en-->aaaaaaaaaa', 398);
INSERT INTO `indextable` VALUES (398, 'description', '<!--:en-->aaaaaaaaaa', 398);
INSERT INTO `indextable` VALUES (398, 'city', 'localhost', 398);

SELECT distinct post_id FROM indextable as i WHERE ( i.key ='price' AND i.value BETWEEN 12 AND 1000 ) AND ( i.key ='year' AND i.value=2012 )

注意:当我只使用第一个条件或只使用第二个条件时,我会得到一个结果。

我想执行这样的查询:

SELECT distinct post_id 
FROM indextable as i 
WHERE ( i.key ='price' AND i.value BETWEEN 12 AND 1000 )
    AND ( i.key ='year' AND i.value=2012 )
    AND ( i.key ='body_condition' AND i.value=1 )
    AND ( i.key ='mechanical_condition' AND i.value=6 )
    AND ( i.key ='seller_type' AND i.value=11 )
    AND ( i.key ='body_type' AND i.value=347 )
    AND ( i.key ='doors' AND i.value=360 )
    AND ( i.key ='no_of_cylinders' AND i.value=365 )
    AND ( i.key ='color' AND i.value=707 )
    AND ( i.key ='transmission_type' AND i.value=377 )
    AND ( i.key ='horsepower' AND i.value=387 )
    AND ( i.key ='warranty' AND i.value=725 )
    AND ( i.key ='fuel_type' AND i.value=380 ) 
4

2 回答 2

1

那是因为你的条件不对。

    SELECT distinct post_id FROM indextable as i 
WHERE ( i.key ='price' AND i.value BETWEEN 12 AND 1000 ) 
AND ( i.key ='year' AND i.value=2012 )

这和使用是一样的

    SELECT distinct post_id FROM indextable as i 
WHERE  i.key ='price' AND i.value BETWEEN 12 AND 1000 
AND  i.key ='year' AND i.value=2012

我认为您尝试做的是

    SELECT distinct post_id FROM indextable as i 
WHERE ( i.key ='price' AND i.value BETWEEN 12 AND 1000 ) 
OR ( i.key ='year' AND i.value=2012 )

编辑:你的意思是你没有这个结果?我复制了您的表格并进行了查询,并且可以正常工作。

在此处输入图像描述

于 2012-06-01T18:51:08.580 回答
0
 .... WHERE ( i.key ='price' AND i.value BETWEEN 12 AND 1000 ) AND 
            ( i.key ='year' AND i.value=2012 )

意味着 i.key 的值必须是 'price' 和 'year' 在同一行。这是不可能的,所以没有结果!

改成

 .... WHERE ( i.key ='price' AND i.value BETWEEN 12 AND 1000 ) OR 
            ( i.key ='year' AND i.value=2012 )
于 2012-06-01T18:51:05.207 回答