我想在MySQL中使用SELECT
語句,並且有兩個AND
條件。這是我的表和插入,然後是選擇語句。我沒有從select語句中得到任何結果。SELECT with two AND條件
--
-- 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)
按設計工作,不是嗎?沒有一次記錄符合兩個條件。您可能正在尋找'OR'而不是'AND'? –
@ user1431393我如何說你需要改變......)和(...爲OR,所以它會是(....)或(....)或(...)或.. ... – jcho360