[英]Combine SQL query result with another query statement under one query? (Complicated)
I currently want to combine two SQL queries into one. 我目前想将两个SQL查询合并为一个。 This is a bit similar to SQL: Taking the result of of query and using it another - combine .
这有点类似于SQL:获取查询结果,然后另一个使用它-Combine 。 Suppose there are two queries:
假设有两个查询:
SQL Statement
SQL语句
1.) SELECT *
FROM (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B ON A.ID = B.ID
WHERE A.ABC ='ABC'
ORDER BY A.ORDER_BY ) as colstate
2.) SELECT colstate
FROM EXAMPLE_TABLE
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
I want to use the result in query 1.) as the colstate (column statement) in query 2.). 我想将查询1.)中的结果用作查询2.)中的colstate(列语句)。 But:
但:
What Have I tried is:
我试过的是:
SELECT (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B
ON A.ID = B.ID
WHERE A.ABC ='ABC'
ORDER BY A.ORDER_BY )
FROM EXAMPLE_TABLE
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
And it turns out to be Error: Scalar subquery is only allowed to return a single row , how should I replace the "=" into "IN"? 原来是错误:标量子查询只允许返回一行 ,我该如何将“ =”替换为“ IN”? Or is my statement totally wrong?
还是我的说法完全错误?
The error says that query you are using as column statement must return at most a single row. 该错误表明您用作列语句的查询最多只能返回一行。
It should probably look something like this: 它可能看起来像这样:
SELECT (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B
ON A.ID = B.ID
WHERE A.ABC ='ABC'
AND A.SOME_COLUMN = E.SOMECOLUMN // retrieve only relevant data for this row
ORDER BY A.ORDER_BY )
FROM EXAMPLE_TABLE E
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
"Combine two queries into one" - that's not a good specs. “将两个查询合并为一个”-这不是一个很好的规范。 Try to find out what exactly you want to get as a FLAT 2-dimensional table, think of nested SELECTs as of nested loops where the inner one can only set a single value for parent's row.
尝试找出要作为FLAT二维表真正获得的内容,将嵌套SELECT视为嵌套循环,其中内部循环只能为父行设置单个值。 Like this:
像这样:
[Outer loop - parent row]
[Inner loop - children rows]
// all you can do here is change a single parent's field to anything
// like constant/sum/avg/topmost/ugly-subquery-returning-a-single-result
[/Inner loop]
[/Outer loop]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.