mysql IF Else Statement [英] mysql IF Else Statement

查看:118
本文介绍了mysql IF Else Statement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定if / else语句的sql查询可以使用多远。

Not sure how far a sql query can go with if/else statements.

我有一个简单的SELECT语句:

I have a simple SELECT statement:


SELECT 金额 transtype FROM
交易

transtype 列将是一个数字。

例如,1 =销售,2 =退款,3 =错误,4 =取消,5 =其他......等等。

For example, 1 = sale, 2 = refund, 3 = error, 4 = canceled, 5 = something else.... and so on.

所以,没什么复杂的。但由于报告原因,该列表往往会增长。哪个没问题。

So, nothing complicated. But the list tends to grow for reporting reasons. Which is fine.

对于我正在处理的特定查询,有没有办法将该列提取为2个或3个指定数字或文本之一?

For a specific query I'm working on, is there a way to extract that column as one of 2 or three specified numbers or text?

例如,某些transstype数字是'亏损',而其他的是'收益',而其他可能是'中立'。

For example, some transtype numbers are a 'loss', while others are a 'gain', and maybe others are 'neutral'.

我想只用那些3提取那个列,而不是在html表中使用php我把行扔进去。

I'd like to extract that column with only those 3, without using php inside the html table I'm throwing the rows into.

如果我的解释不明确,我道歉。吐出来很难。

If my explanation is not clear, my apologies. It was hard to spit out.

推荐答案

使用 MySQL CASE()函数用于固定数量的参数。如果列表变大,你应该使用第二个表并加入它们。

Use the MySQL CASE() function for a fixed number of arguments. If the list is getting big, you should use a second table and join them.

示例:

SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;

这篇关于mysql IF Else Statement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆