gpt4 book ai didi

mysql - 我如何 SQL Join 2个数据库,在一列中只有不同的结果

转载 作者:行者123 更新时间:2023-11-30 22:45:43 24 4
gpt4 key购买 nike

我正在使用交互式 SQL 教程(您可以从 http://sol.gfxile.net/ 中获取结构)来帮助我解决现实世界中的问题。我在左表中获得不同的结果时遇到问题,而在右 SQL 表中仍然返回非空结果。这是我的尝试和结果:

SELECT stars.name as sn, planets.name 
FROM stars LEFT OUTER JOIN planets
ON stars.starid=planets.starid

sn name
O-00000 (null)
G-00001 (null)
M-00002 (null)
K-00003 (null)
G-00004 (null)
G-00005 G-00005/A
G-00005 G-00005/B

在尝试 1 中,我在“名称”列中得到了结果,但在我的“sn”列中出现了错误,因为一些恒星有多个行星。尝试 2:

SELECT stars.name, planets.name 
FROM stars LEFT OUTER JOIN planets
ON stars.starid=planets.starid
where not exists (
select 1 from stars
where stars.starid = planets.starid
)

name name
O-00000 (null)
G-00001 (null)
M-00002 (null)
K-00003 (null)
G-00004 (null)

在尝试 2 中,我在第 1 列中获得了唯一记录,但在第 2 列中获得了所有空值。我如何才能在第 1 列中获得唯一记录,并在第 2 列中获得非空值?

最佳答案

不确定您的目标是什么,但请尝试:

SELECT COALSCE(planets.name,stars.name) as name  
FROM stars LEFT JOIN planets
ON stars.starid=planets.starid
GROUP BY stars.starid

或者你想要

SELECT stars.name as sn, planets.name  
FROM stars LEFT JOIN planets
ON stars.starid=planets.starid
GROUP BY stars.starid

关于mysql - 我如何 SQL Join 2个数据库,在一列中只有不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676295/

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