是否可以在单个SQL查询中将三个单独的选择放在一起,其中两个选择抓取第一个选择的数据?

发布于 2025-02-07 18:08:08 字数 891 浏览 1 评论 0原文

如果我措辞最初的问题,希望这个描述清除了它。我有一个类似选择的SQL查询,所以

select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
inner join DATABASETWO.table2 notmaster
on master.column = notmaster.column2
where master.number = 55555
and notmaster.column3 in (~~~DATA I'M MISSING AND NEED~~~)

现在我的问题是我需要在此SQL查询期间获取一个整数列表(查询底部缺少的数据IM丢失),我相信我需要两个选择来除了初始选择之外,请执行此操作。我还有另外两个我需要使用的表格,首先我需要使用ID(ID可以是初始查询中的55555)才能从第一表中获取一个值,

select secondValue as identifier from THEDB.THETABLE foo
where foo.id = 55555

然后使用该值,我们只是使用该值得到,我将在第二个表中寻找一场比赛,然后从类似的行中拿起一个匹配的行中获取一个值。

select valuesNeeded as listofvalues from THEOTHERDB.TABLE bar
where bar.value = identifier

我们抓取的名为ListOfValues的值将是第一个选择中我需要的整数列表,因此,一旦被抓住,我就可以在部分过程中使用该值。有没有办法在严格的SQL和一个查询中进行此操作?我可以更改代码,但是当前对我来说,仅更改SQL查询本身要容易得多。感谢您的任何帮助/指示!

If I worded the initial question poorly hopefully this description clears it up. I have a SQL query that is a SELECT like so

select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
inner join DATABASETWO.table2 notmaster
on master.column = notmaster.column2
where master.number = 55555
and notmaster.column3 in (~~~DATA I'M MISSING AND NEED~~~)

Now my issue is that I need to get a list of integers (The DATA IM MISSING at the bottom of the query) during this SQL query and I believe I'll need two SELECTS to do it in addition to the initial SELECT. I have two other tables I'd need to work with, first I'll need to use an id (The ID can be the 55555 in the initial query) to grab a value from the first table like so

select secondValue as identifier from THEDB.THETABLE foo
where foo.id = 55555

Then using that value we just got, I'll look for a match in the second table, then grab a value from the row it found a match on like so.

select valuesNeeded as listofvalues from THEOTHERDB.TABLE bar
where bar.value = identifier

That value we grabbed called listofvalues will be the list of integers I'll need in the first SELECT, so once that is grabbed, I can use that value in the initial SELECT during the IN part. Is there a way to do this in strictly SQL and in one query? I can make code changes, but just changing the SQL query itself is much easier for me currently. Thanks for any help/pointers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2025-02-14 18:08:08

您可以使用with功能来创建以后可以使用的“临时”表。

这样的东西也许?

WITH identifier AS (
  select secondValue as identifier 
  from THEDB.THETABLE foo
  where foo.id = 55555
), valuelist AS (
  select valuesNeeded as listofvalues 
  from THEOTHERDB.TABLE bar
  where bar.value in (SELECT identifier FROM identifier)
)
select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
inner join DATABASETWO.table2 notmaster on master.column = notmaster.column2
where master.number = 55555
  and notmaster.column3 in (SELECT listofvalues FROM valuelist)

您也可以进行子查询 - 在功能上与上述相同:

select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
inner join DATABASETWO.table2 notmaster on master.column = notmaster.column2
where master.number = 55555
  and notmaster.column3 in (
    select valuesNeeded as listofvalues 
    from THEOTHERDB.TABLE bar
    where bar.value in (
      select secondValue as identifier 
      from THEDB.THETABLE foo
      where foo.id = 55555
    )
  )

但是SQL中的最佳方法是加入

select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
join DATABASETWO.table2 notmaster on master.column = notmaster.column2
join THEOTHERDB.TABLE bar on notmaster.column3 = bar.valuesNeeded
join THEDB.THETABLE foo on foo.id = 5555 AND bar.value = foo.secondValue
where master.number = 55555

You can use the WITH feature to create "temporary" tables that can be used later.

Something like this maybe?

WITH identifier AS (
  select secondValue as identifier 
  from THEDB.THETABLE foo
  where foo.id = 55555
), valuelist AS (
  select valuesNeeded as listofvalues 
  from THEOTHERDB.TABLE bar
  where bar.value in (SELECT identifier FROM identifier)
)
select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
inner join DATABASETWO.table2 notmaster on master.column = notmaster.column2
where master.number = 55555
  and notmaster.column3 in (SELECT listofvalues FROM valuelist)

You can also do sub queries -- functionally the same as above:

select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
inner join DATABASETWO.table2 notmaster on master.column = notmaster.column2
where master.number = 55555
  and notmaster.column3 in (
    select valuesNeeded as listofvalues 
    from THEOTHERDB.TABLE bar
    where bar.value in (
      select secondValue as identifier 
      from THEDB.THETABLE foo
      where foo.id = 55555
    )
  )

But the best way to this in SQL is with joins

select tester as TESTNAME, testing as TESTNAMETWO, test as TESTNAMETHREE
from DATABASE.table master
join DATABASETWO.table2 notmaster on master.column = notmaster.column2
join THEOTHERDB.TABLE bar on notmaster.column3 = bar.valuesNeeded
join THEDB.THETABLE foo on foo.id = 5555 AND bar.value = foo.secondValue
where master.number = 55555
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文