gpt4 book ai didi

mysql - 当外键相似时如何获取单行

转载 作者:行者123 更新时间:2023-11-29 07:40:47 26 4
gpt4 key购买 nike

我有一个包含列的表 tbl_issue

> serial_no.
Issue_no. (f.k)
From_Section
To_Section
+-----+---------------+-------------+-------------+
| id | issue no | from section| to_section |
+-----+---------------+-------------+-------------+
| 1 | 223 | MFA | N/A |
| 2 | 223 | N/A | LOG |
+----------+----------+-------------+--------------+

当我查询第 1 期的表时。我得到两行,任何人都可以帮助我如何获得一条记录并且没有“N/A”

最佳答案

对于您给出的示例,这可行:

WITH combined AS
(
SELECT i.issue_no,
CASE WHEN i.from_section = 'N/A' THEN i2.from_section ELSE i.from_section END from_section,
CASE WHEN i.to_section = 'N/A' THEN i2.to_section ELSE i.to_section END to_section
FROM dbo.tbl_issue i
INNER JOIN dbo.tbl_issue i2
ON i2.issue_no = i.issue_no
)

SELECT DISTINCT *
FROM combined c
WHERE c.from_section <> 'N/A' AND c.to_section <> 'N/A'

假设“N/A”并不意味着 NULL...如果您的意思是 NULL,请将“= 'N/A'”替换为 IS NULL,并将“<> 'N/A'”替换为 IS不为空

关于mysql - 当外键相似时如何获取单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29122518/

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