MySQL:选择一个额外的列,根据 IF 语句对另一列进行分类
假设这个命令:SELECT sessionID, SessionLength FROM mytable;
生成这个表:
+-----------+---------------+
| sessionID | SessionLength |
+-----------+---------------+
| 1 | 00:20:31 |
| 2 | 00:19:54 |
| 3 | 00:04:01 |
...
| 7979 | 00:00:15 |
| 7980 | 00:00:00 |
| 7981 | 00:00:00 |
+-----------+---------------+
7981 rows in set (0.92 sec)
但我想生成一个像这样的表:
+-----------+---------------+--------+
| sessionID | SessionLength | Size |
+-----------+---------------+--------+
| 1 | 00:20:31 | BIG |
| 2 | 00:19:54 | BIG |
| 3 | 00:04:01 | MEDIUM |
...
| 7979 | 00:00:15 | SMALL |
| 7980 | 00:00:00 | SMALL |
| 7981 | 00:00:00 | SMALL |
+-----------+---------------+--------+
7981 rows in set (0.92 sec)
- 当
SessionLength > 时,某物是
大
。 10 - 当
SessionLength <= 10 AND SessionLength >=1
时,某物为中
; - 当其
时,某物为
小
会话长度> 1
从概念上讲,我想做的是:
SELECT
sessionID,
SessionLength,
(SessionLength > 10 ? "BIG" : (SessionLength < 1 : "SMALL" : "MEDIUM"))
FROM mytable;
有没有一种简单的方法可以做到这一点?
Suppose this command: SELECT sessionID, SessionLength FROM mytable;
Generates this table:
+-----------+---------------+
| sessionID | SessionLength |
+-----------+---------------+
| 1 | 00:20:31 |
| 2 | 00:19:54 |
| 3 | 00:04:01 |
...
| 7979 | 00:00:15 |
| 7980 | 00:00:00 |
| 7981 | 00:00:00 |
+-----------+---------------+
7981 rows in set (0.92 sec)
But I want to generate a table like this:
+-----------+---------------+--------+
| sessionID | SessionLength | Size |
+-----------+---------------+--------+
| 1 | 00:20:31 | BIG |
| 2 | 00:19:54 | BIG |
| 3 | 00:04:01 | MEDIUM |
...
| 7979 | 00:00:15 | SMALL |
| 7980 | 00:00:00 | SMALL |
| 7981 | 00:00:00 | SMALL |
+-----------+---------------+--------+
7981 rows in set (0.92 sec)
- Something is
big
when it'sSessionLength > 10
- Something is
medium
when it'sSessionLength <= 10 AND SessionLength >=1
- Something is
small
whne it'sSessionLength > 1
Conceptually what I want to do is this:
SELECT
sessionID,
SessionLength,
(SessionLength > 10 ? "BIG" : (SessionLength < 1 : "SMALL" : "MEDIUM"))
FROM mytable;
Is there an easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
Yes,
华泰
HTH