外键相似时如何获得单行

时间:2015-03-18 12:48:24

标签: mysql sql database oracle

我有一个带有列

的表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         |
+----------+----------+-------------+--------------+

当我在问题上查询表时没有。我得到两行,任何人都可以帮助我如何获得单个记录而没有'N / A'

1 个答案:

答案 0 :(得分:2)

对于您给出的示例,这将起作用:

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&#39;并不意味着NULL ...如果你的意思是NULL替换&#34; =&#39; N / A&#39;&#34;使用IS NULL并替换&#34;&lt;&gt; &#39; N / A&#39;&#34; IS NOT NULL