【问题标题】:Find certain values and show corresponding value from different field in SQL从 SQL 中的不同字段中查找某些值并显示相应的值
【发布时间】:2017-11-01 14:03:50
【问题描述】:

所以我找到了这两篇文章,但它们并没有完全回答我的问题......

Find max value and show corresponding value from different field in SQL server

Find max value and show corresponding value from different field in MS Access

我有一张这样的桌子...

ID      Type        Date
1       Initial      1/5/15 
1       Periodic     3/5/15
2       Initial      2/5/15  
3       Initial      1/10/15
3       Periodic     3/6/15  
4        
5       Initial      3/8/15

我需要获取所有“定期”或 NULL 的 ID 号和相应的日期。所以我想要一个看起来像这样的查询结果......

ID     Type    Date
1    Periodic  3/5/15
3    Periodic  3/6/15
4

我试过了

select id, type, date1
from Table1 as t
where type in (select type
               from Table1 as t2
               where ((t2.type) Is Null) or "" or ("periodic"));

但这不起作用...从我读到的有关 NULL 的内容中,您无法比较 null 值... Why in SQL NULL can't match with NULL?

所以我尝试了

SELECT id, type, date1
FROM Table1 AS t
WHERE type in (select type
               from Table1 as t2
               where ((t.Type)<>"Initial"));

但这并没有给我 4 的 ID...

有什么建议吗?

【问题讨论】:

  • 根据你的用户名,我猜正确的标签是 ms-access。

标签: sql ms-access


【解决方案1】:

除非我遗漏了什么,否则你只需要:

select id, type, date1
from Table1 as t
where (t.type Is Null) or (t.type = "") or (t.type = "periodic");

or 适用于布尔表达式,而不适用于被比较的值。

【讨论】:

  • 感谢您的回复!是的,但是如果我添加到原始数据集“4 Periodic 3/6/15”,我会得到“4 Periodic 3/6/15”和“4”......所以如果两者都有,我需要只是得到“4 Periodic 3/6/15”......这就是我试图匹配它们的原因......
猜你喜欢
  • 2015-11-19
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多