MySQL - 条件查询,语句结果
我想构造一个这样的查询:
SELECT * FROM t1 WHEREvisible='yes' AND IF (sale = 'yes', amount > 0)
我的目标是仅接收表中数量大于的行0 仅当它是“销售”产品时。我尝试了 IF 和 CASE 语句,但似乎这些语句的结果只能是一串整数,而不是语句。
我被困在这里了...
I want to construct a query like this:
SELECT * FROM t1 WHERE visible='yes' AND IF (sale = 'yes', quantity > 0)
It is my goal is to receive only the rows from the table with quantity more then 0 only when it is a 'sale' product. I tried IF and CASE statements but it seems that the result of these can only by a string of integer, not a statement.
I am stuck here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您阅读他的原文“仅当它是“销售”产品时才从表中接收数量大于 0 的行”,
因为这个问题的措辞有些糟糕。假设是:
仅返回 SALE='yes' 且 QUANTITY > 的商品。 0 且可见=“是”。
如果这是用户想要的,那么 SQL 很简单:
顺便说一句,如果您的数据库要保存大量数据,使用 booleans(true/false) 而不是 varchar 在引擎中效率更高,并且会节省选择和操作的时间。插入。
If you read his original text " receive only the rows from the table with quantity more then 0 ONLY when it is a 'sale' product"
Since the question is somewhat poorly phrased. The assumption is:
Return ONLY items where SALE='yes' and QUANTITY > 0 and VISIBLE='yes'.
If this is what the user wants, the SQL for is simply:
BTW, if your DB is going to hold much data, using booleans(true/false) instead of varchars is much more efficient in the engine and will save time on selects and inserts.
您缺少
IF
函数中的第三个参数是有效的 SQL
You are missing the 3rd paramater inthe
IF
functionis valid SQL