【发布时间】:2018-08-04 11:45:49
【问题描述】:
我有一本书,里面有数百名学生的记录,每个学生都有一个身份证号码,后跟名字和姓氏。
我需要提取更多信息,例如出生日期、性别等。为此,我有一个 Student 表,其中存储了我需要的所有信息(因此我不需要任何 JOIN与其他表),所以我想在 excel 中执行 rune 函数,将查询连接起来,然后是 UNION:
=CONCATENAR("SELECT * FROM (SELECT TOP 1 Std_Identification, Std_Gender, Std_BirthDate FROM Student WHERE Std_Identification='";A1;"') AS T UNION ALL")
但是,标识号可能已过时,因此记录不完整(如果我用 200 条记录进行查询,他们可以留下 190 条),它们的差异很小,但我需要多次执行此任务,我想要的是这个:
+--------------------+------------+---------------+
| Std_Identification | Std_Gender | Std_BirthDate |
+--------------------+------------+---------------+
| 34998545 | 0 | 12/05/1997 |
+--------------------+------------+---------------+
| 12443334 | NULL | NULL | <- This record NOT exists in the table Student
+--------------------+------------+---------------+
| 39405443 | 1 | 21/09/1980 |
+--------------------+------------+---------------+
我尝试了以下查询,但仍然没有成功:
SELECT * FROM (SELECT TOP 1 Std_Identification, Std_Gender, Std_BirthDate FROM Student WHERE Std_Identification='34998545') AS T UNION ALL
SELECT * FROM (SELECT TOP 1 Std_Identification, Std_Gender, Std_BirthDate FROM Student WHERE Std_Identification='12443334') AS T UNION ALL
SELECT * FROM (SELECT TOP 1 Std_Identification, Std_Gender, Std_BirthDate FROM Student WHERE Std_Identification='39405443') AS T
但结果是:
+--------------------+------------+---------------+
| Std_Identification | Std_Gender | Std_BirthDate |
+--------------------+------------+---------------+
| 34998545 | 0 | 12/05/1997 |
+--------------------+------------+---------------+
| 39405443 | 1 | 21/09/1980 |
+--------------------+------------+---------------+
- 如何让 SELECT UNION 显示 NULL 而不是空白 什么时候记录不存在?
- 是否可以这样做,但是当我需要一个 JOIN 与多个 表?假设我需要提取 在另一个表 X 中(因此由于在 表 Student 和表 X),但并非所有学生都有代码或 记录以将其与该表相关联X
【问题讨论】:
-
尝试使用 [(select 34998545 ID ) left join Std_identification]
标签: sql sql-server database select null