有条件选择 [英] Select with condition

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

问题描述

我需要有关选择条件的帮助,现在我正在这样做:

I need some help about select with condition, right now I'm doing this:

                GROUP_CONCAT(
                    CASE WHEN glpi_tickets.users_id_lastupdater = glpi_users.id THEN
                        CONCAT(glpi_users.firstname, ' ', glpi_users.realname) 
                    END SEPARATOR '<br>') AS last_updater

当users_id_lastupdater = id时我选择名字和实名

I select firstname and realname when users_id_lastupdater = id

我想有一个更好的方法吗?

I guess there is a better way to do this?

推荐答案

group_concat 似乎对此有些大意。您可以只使用 max()

group_concat seems like overkill for this. You could just use max():

max(CASE WHEN glpi_tickets.users_id_lastupdater = glpi_users.id
         THEN CONCAT(glpi_users.firstname, ' ', glpi_users.realname) 
    END) AS last_updater

不需要分隔符,因为它不仅用于单个元素。

The separator isn't needed because it isn't used for only one element.

编辑:

max()函数采用参数值的最大值。在这种情况下,这是有条件的,因为 case 。如果不满足条件,则值为 NULL (没有 else 子句)。因此,它在满足条件时检索值。如果多行符合条件,它将获取最大值。

The max() function takes the maximum of value of an argument. In this case, it is conditional because of the case. When the condition is not met, the values are NULL (no else clause). So, it retrieves the value when the condition is met. If multiple row match the condition, it retrieves the largest value.

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

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