gpt4 book ai didi

mysql - 合并 MySQL 中的整个记录

转载 作者:行者123 更新时间:2023-11-30 23:07:14 24 4
gpt4 key购买 nike

我目前正在 MySQL 查询中单独合并字段,但我想合并整个记录。

这可能吗?

SELECT la.id, 
COALESCE(( SELECT name FROM lookup_changed l0,
( SELECT MAX(id) id
FROM lookup_changed
WHERE lookup_id = 26
) l1
WHERE l0.id = l1.id
), la.name) name,
COALESCE(( SELECT msisdn FROM lookup_changed l0,
( SELECT MAX(id) id
FROM lookup_changed
WHERE lookup_id = 26
) l1
WHERE l0.id = l1.id
), la.msisdn) msisdn
FROM lookup_added la
WHERE la.id = 26

@Alma Do - 伪 SQL 是:

SELECT la.id, 
MULTICOALESCE(( SELECT <name, msisdn> FROM lookup_changed l0,
( SELECT MAX(id) id
FROM lookup_changed
WHERE lookup_id = 26
) l1
WHERE l0.id = l1.id
), <la.name, la.msisdn>) <name, msisdn>
FROM lookup_added la
WHERE la.id = 26

最佳答案

由于 COALESCE()“返回第一个非 NULL 参数”,听起来您想检索“查询集中的第一个非 NULL 结果”:

-- syntax error
SELECT COALESCE(
SELECT a FROM ta,
SELECT b FROM tb
);

-- roughly equates to
( SELECT a AS val FROM ta WHERE a IS NOT NULL ORDER BY a LIMIT 1 )
UNION
( SELECT b AS val FROM tb WHERE b IS NOT NULL ORDER BY b LIMIT 1 )
ORDER BY val LIMIT 1 ;

评论:

  • 我添加了 ORDER BY 子句,否则“第一行”没有任何意义
  • 内部的 LIMIT 1 子句是可选的(但允许提前修剪子结果)
  • 子查询周围的括号是强制性的

关于mysql - 合并 MySQL 中的整个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21331094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com