首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL如果id匹配,则从第二个表中选择日期

SQL如果id匹配,则从第二个表中选择日期
EN

Stack Overflow用户
提问于 2013-03-17 03:25:24
回答 3查看 539关注 0票数 0

我有两个表Security和CallPut。security (table1)包含security_id和maturity_date (非NULL值),Callput(table2)包含security_id和odate (它们是包含日期时间格式值的非NULL值)我想从security (表1)中选择所有行,但在maturity_date中,我想要Odate列中的值,而不是maturity_date,其中Security id存在于CallPut(table2)中并与security表匹配

代码语言:javascript
复制
Security_id|Maturity_date
abcd        25Jan2020
bcde        25jan2021 

Callput(tabble2)

代码语言:javascript
复制
Security_id|Odate
abcd        25Jan2015 

所需输出

代码语言:javascript
复制
Security_id|Maturity_date
abcd        25Jan2015 (since here id is there in callput it takes odate)
bcde        25jan2021 (since here id is not there in callput it takes maturity date)

提前感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-17 03:28:49

代码语言:javascript
复制
select s.Security_id, nvl(c.odate, s.Maturity_date) as Maturity_date
from Security s
left join
Callput c
on
s.Security_id = c.Security_id

附注:我刚注意到这是一个Oracle问题,我的代码是针对MS SQL Server的

编辑:已针对Oracle语法进行更正。

票数 0
EN

Stack Overflow用户

发布于 2013-03-17 03:38:29

代码语言:javascript
复制
select 
    s.Security_id,
    NVL(c.Odate, s.Maturity_date) Maturity_date
from 
    security s left join callput c 
    on (c.security_id = s.security_id);
票数 1
EN

Stack Overflow用户

发布于 2013-03-17 03:28:40

代码语言:javascript
复制
select s.* from security as s left join callput as c on (c.security_id = s.security_id);

未测试,但您想要使用联接。您可以在http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries006.htm的Oracle内容丰富的文档中了解有关连接的更多信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15453671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档