mysql - 如何应用规则根据条件和分组数据更新字段中的记录
问题描述
我正在尝试在 sql 中执行此任务。但我无法得出有助于获得此输出的逻辑。
解决方案
It looks like you want to bring the name of the player whose code is "x" for each id. If so, one option uses window functions:
select id, playercode, playername,
max(case when playercode = 'x' then playername end) over(partition by id) requiredoutput
from mytable
If your database does not support window functions, one option uses a correlated subquery. Assuming no duplicates on (id, playercode)
:
select id, playercode, playername,
(
select t1.playername
from mytable t1
where t1.id = t.id and t1.playercode = 'x'
) requiredoutput
from mytable t
推荐阅读
- javascript - 如何在本机反应中通过 Gmail API 对附件进行可恢复上传?
- css - CSS不适用于django密码表单字段
- javascript - 将 BrowserRouter 替换为 HashRouter
- scala - 如何为 Map[(Int, Int), A] 编写 play-json Reads?
- sql - 使用存储过程中查询中项目的索引位置更新列
- c++ - 'const char*' 和 'WCHAR*'
- javascript - React 原生 UI 设计还是 android ui 设计?
- python - 如何为使用烧瓶的网站创建一个使用 python 是/否对话文本和烧瓶 html 文本框的文本框?
- database - 如何正确关联用户与 Redis 中的数据并根据日期字段触发更新
- reactjs - 使用 useState 钩子的输入元素的 onChange 属性