除了mysql查询中的条件..

时间:2012-01-26 08:39:20

标签: php mysql

我有以下结构的表格,

name (type: TEXT)
price (type: INT)
color (type: TEXT)
vehicletype (type: TEXT) eg. SEDAN/SUV 

现在我要检索(名称,价格,颜色,车型)如果它是黑色轿车,我准备支付10,000,但如果它是红色或白色,那么不超过8,000。对于任何其他颜色,我不会超过7,000,除非它是SUV,在这种情况下,我的预算是黑色的15,000或任何其他颜色的14,000。 我有这个查询,但它没有用,

SELECT name, price, color, vehicletype FROM carrecords WHERE
(vehicletype = 'SEDAN' AND color  = 'black' AND price <= 10000) OR
(vehicletype = 'SEDAN' AND color IN('red','white') AND price <= 8000 ) OR
(vehicletype  = 'SEDAN' /* here I tried != "SUV" as well - no luck*/  AND color NOT IN('red','white','black') AND price <= 7000) OR
(vehicletype = 'SUV' AND color  = 'black' AND price <= 15000) OR
(vehicletype = 'SUV' AND color  != 'black' AND price <= 14000)
ORDER BY price ASC  

所以,有什么解决方案吗?

1 个答案:

答案 0 :(得分:4)

SELECT name, price, color, vehicletype FROM carrecords WHERE
-- black sedans up to 10k
(vehicletype = 'SEDAN' AND color  = 'black' AND price <= 10000)
-- red or white sedans up to 8k
OR (vehicletype = 'SEDAN' AND color IN('red','white') AND price <= 8000 )
-- black SUV up to 15k
OR (vehicletype = 'SUV' AND color  = 'black' AND price <= 15000)
-- non-black SUV up to 14k
OR (vehicletype = 'SUV' AND color  != 'black' AND price <= 14000)
-- any other vehicle up to 7k
OR (price <= 7000)
ORDER BY price ASC