暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 从表中选择一行

askTom 2017-10-18
279

问题描述

嗨,

我有一个表如下:

创建表测试 (名称varchar2(100),VAL varchar2(50))
表中的值如下:

插入测试 (名称,VAL) 值 (“邮件”,“jayas.muntimad@oracle.com”)
插入测试 (名称,VAL) 值 ('uid','jmunma')

插入测试 (名称,VAL) 值 (“邮件”,“apua.shma@oracle.com”)
插入测试 (名称,VAL) 值 ('uid','apuarm')

插入测试 (名称,VAL) 值 (“邮件”,“subhash.k.kumar@oracle.com”)
插入测试 (名称,VAL) 值 ('uid','subhasku')

我需要一个查询,比如当我给VAL作为jmunma,它应该从上面的行返回VAL作为jayas.muntimad@oracle.com,当我给VAL作为apuarm,它应该从上面的行返回VAL作为apua.shma@oracle.com,当我给VAL作为subhasku时,它应该从上面的行返回VAL作为subhash.k.kumar@oracle.com。

在上表中,有2行之间的关系。像第一行 (具有电子邮件id) 和第二行 (具有uid) 具有单个用户的记录。第三行和第四行是针对单个用户的,类似地,第五行和第六行具有用户的记录。
我有uid,想获得相应的电子邮件。

请让我知道相同的适当查询。

谢谢,
子哈希

专家解答

您需要 * 一些 * 列链接到两行一起,否则没有什么可说的第1行没有链接到第3行或第7行等...

所以我假设下面的测试用例中有一个 “ID” 列-然后你可以做一个联接

SQL> create table test(id int, NAME varchar2(100), VAL varchar2(50));

Table created.

SQL>
SQL> insert into test(id,NAME,VAL) values(1,'mail','jayas.muntimad@oracle.com');

1 row created.

SQL> insert into test(id,NAME,VAL) values(1,'uid','jmunma');

1 row created.

SQL> insert into test(id,NAME,VAL) values(2,'mail','apua.shma@oracle.com');

1 row created.

SQL> insert into test(id,NAME,VAL) values(2,'uid','apuarm');

1 row created.

SQL> insert into test(id,NAME,VAL) values(3,'mail','subhash.k.kumar@oracle.com');

1 row created.

SQL> insert into test(id,NAME,VAL) values(3,'uid','subhasku');

1 row created.

SQL> select t2.val
  2  from   test t1, test t2
  3  where  t1.val = 'apuarm'
  4  and    t1.name = 'uid'
  5  and    t2.id = t1.id
  6  and    t2.name = 'mail';

VAL
--------------------------------------------------
apua.shma@oracle.com

1 row selected.


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论