MySQL:选择一个额外的列,根据 IF 语句对另一列进行分类

发布于 2024-10-02 16:34:41 字数 1364 浏览 1 评论 0原文

假设这个命令: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's SessionLength > 10
  • Something is medium when it's SessionLength <= 10 AND SessionLength >=1
  • Something is small whne it's SessionLength > 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-10-09 16:34:41

是的,

SELECT
   sessionID, SessionLength,
   CASE WHEN SessionLength > 10 THEN 'BIG'
        WHEN SessionLength < 1 THEN 'SMALL'
        ELSE 'MEDIUM'
   END 
FROM mytable;

Yes,

SELECT
   sessionID, SessionLength,
   CASE WHEN SessionLength > 10 THEN 'BIG'
        WHEN SessionLength < 1 THEN 'SMALL'
        ELSE 'MEDIUM'
   END 
FROM mytable;
心的位置 2024-10-09 16:34:41
SELECT
    sessionID,
    SessionLength,
    IF( SessionLength > 10, "BIG",
        IF( SessionLength < 1, "SMALL", "MEDIUM")) AS Size
FROM mytable;

华泰

SELECT
    sessionID,
    SessionLength,
    IF( SessionLength > 10, "BIG",
        IF( SessionLength < 1, "SMALL", "MEDIUM")) AS Size
FROM mytable;

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文