如何从基于“偏好”的表格中获取价值 [英] How to get value from a table base on "Preference"

查看:145
本文介绍了如何从基于“偏好”的表格中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从基于偏好的表中获取值。

I am attempting to get a value from a table based on "Preference".

我有一个表t1,包括两列id和alphabet

I have a table "t1" including two columns "id" and "alphabet"

样本数据

1 A
1 B
2 A
3 A
3 B

这意味着每一个id具有默认值A,而某些具有B)。

It means every id has the default value "A" and some have "B").

现在我想根据B的偏好得到id的值。因此,如果ID同时具有A和B,我想要B行。如果没有找到B行,那么应该返回默认的A.

Now I would like to get the values of "id" based on preference of "B". Thus, if an ID has both A and B, I want the B row. If no B row is found, then the default A should be returned.

推荐答案

这可能会稍微关闭,因为mysql方言总是抛出我。由于B按字母顺序排在A之后,如果您对ID进行分组然后选择最大值,它将为您提供存在的所有B行,否则您将获得A行。

This might be slightly off as mysql dialect always throws me. Since B falls after A in an alphabetical sort, if you group on the ID and then select the maximum, it will give you all the B rows where they exist, otherwise you will get the A row.

SELECT
    ID,
    MAX(alphabet) AS preference
FROM
    t1
GROUP BY
    ID

现在,如果您的数据比您指示的更复杂,您将需要检查MAX逻辑是否仍然适用。

Now, if your data is more complex than what you've indicated, you will need to examine whether the MAX logic still applies.

这篇关于如何从基于“偏好”的表格中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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