【问题标题】:Using result of 1st select in union to query the second select使用联合中第一个选择的结果来查询第二个选择
【发布时间】:2019-09-06 10:31:00
【问题描述】:

我需要帮助来进行查询,该查询将使用联合中 select 1 的结果作为查询条件。

示例:

select * from table1 where name='xx'

union all

select * from table1 where Name=(select surname from table2 where surname='xx') -- (the name from first query)

这是我到目前为止得到的,但每次我查询它时,我只得到一行

Select a.client, a.voucher_no from agltaxtrans a left outer join agltaxtrans b on a.voucher_no=b.voucher_no where a.voucher_no=b.voucher_no

union all

select client, voucher_no from agltaxtrans where voucher_no IN (select voucher_cor from a49bontaxtranshist b left outer join agltaxtrans a on a.voucher_no=b.voucher_cor where b.voucher_cor=a.voucher_no)

【问题讨论】:

  • 样本数据、期望的结果和数据库真的很有帮助。
  • 嗨,欢迎来到 StackOverflow!请提供您的表格结构和表格数据、您想要的结果以及您目前得到的结果:)
  • stackoverflow.com/help/mcve,方便为您提供帮助!

标签: sql union outer-join


【解决方案1】:

解决方案是创建另一列并为其命名:

选择名称作为 name_original,名称来自 tabel1

UNION all

select b.name as name_original, a.name as name from table1 a left outer join table2 b on a.city=b.city and a.name=b.surname

这样我可以查询 name_original 并得到想要的结果

感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2021-08-04
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多