gpt4 book ai didi

MySQL连接表而不重复每个表值

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:25 25 4
gpt4 key购买 nike

嘿,我正在尝试连接 2 个表,但左侧映射到所有条目。我正在避免这种情况,因为我希望合并表,但在应有的地方使用 NULL 值

测试表00:

ownership membership
Johnny Active

测试表01:

pet petname
dog John
cat timmy
parrot Johns

当我这样加入他们时:

SELECT * FROM (select * from testtable00 inner join testtable01) as u where u.ownership like "%John%";

ownership membership pet petname
Johnny Active dog John
Johnny Active parrot Johns

我想要实现的是

ownership membership pet petname
Johnny Active NULL NULL
NULL NULL dog John
NULL NULL parrot Johns

最佳答案

因为表之间没有关系,所以您不应该执行 JOIN。您想要的结果看起来像一个 UNION:

SELECT ownership, membership, NULL AS pet, NULL as petname
FROM testtable00
WHERE ownership like '%John%'
UNION ALL
SELECT NULL AS ownership, NULL AS membership, pet, petname
FROM testtable01
WHERE petname like '%John%'

关于MySQL连接表而不重复每个表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45573393/

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