简体   繁体   English

mysql有条件的选择基于销售价格

[英]mysql conditional select based on a sale price

I have a database of products. 我有一个产品数据库。 One of the attributes is a normal price (price), a sale price (sale_price), a sale expiration date (sale_expires) and a sale price that never expires flag (sale_never_expires). 属性之一是正常价格(price),销售价格(sale_price),销售到期日期(sale_expires)和永不过期的销售价格标志(sale_never_expires)。

I can write a nice query that gets me the basic information on the product and the current price: 我可以编写一个不错的查询,以获取有关产品和当前价格的基本信息:

SELECT stock,name, (CASE WHEN (sale_never_expires='Y' AND sale_price > 0.0) OR (sale_expires >= REPLACE(CURDATE(),'-','') AND sale_price > 0.0) THEN sale_price ELSE price END) as current_price FROM inventory WHERE delete_flag=0; 选择股票名称(在(sale_never_expires ='Y'AND sale_price> 0.0)或(sale_expires> = REPLACE(CURDATE(),'-','')AND sale_price> 0.0)THEN sale_price其他价格END的情况下)作为current_price在库存清单中,delete_flag = 0;

But that's not what I need. 但这不是我所需要的。 What I need is to be able to write a query that returns all products in inventory with a current price less than a given amount, say $100 for the sake of example. 我需要的是能够编写一个查询来返回当前价格小于给定金额(例如,为100美元)的库存中的所有产品的查询。 I just need the matching records and will worry about formatting later. 我只需要匹配的记录,以后会担心格式化。

I've tried and tried to find a way to write this but I'm stuck. 我已经尝试并试图找到一种方法来写这个,但我被困住了。 What can I do?? 我能做什么??

Select * from tableName where current price <100 从当前价​​格<100的tableName中选择*

Should work fine unless I've misunderstood. 除非我误解了,否则应该可以正常工作。

一个视图可能会有所帮助..它只会隐藏复杂性..

create view current_products as SELECT stock,name, (CASE ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM