是否可以在单个SQL查询中将三个单独的选择放在一起,其中两个选择抓取第一个选择的数据?
如果我措辞最初的问题,希望这个描述清除了它。我有一个类似选择的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用with功能来创建以后可以使用的“临时”表。
这样的东西也许?
您也可以进行子查询 - 在功能上与上述相同:
但是SQL中的最佳方法是加入
You can use the WITH feature to create "temporary" tables that can be used later.
Something like this maybe?
You can also do sub queries -- functionally the same as above:
But the best way to this in SQL is with joins