[英]Oracle query with subquery from another table in "SELECT"
I have a table1, and table2.我有一个表 1 和表 2。 There are sevaral rows in table2 for ID from table1.表 1 中的 ID 在表 2 中有几行。 Example: Table1 (ID, Name, Age):示例:表 1(ID、姓名、年龄):
543 | John | 15
321 | Doe | 17.
SELECT SCORE
FROM TABLE2
WHERE ID = 543
(many rows as response). (许多行作为响应)。
I need a query with some columns from table1, as well as first row in column from table2.我需要一个包含来自 table1 的某些列以及来自 table2 的列中的第一行的查询。
Something like that:像这样的东西:
SELECT A.NAME NAME,
A.AGE AGE,
(SELECT SCORE
FROM TABLE2 B
WHERE A.ID = B.ID
AND ROWNUM = 1) SCORE
FROM TABLE1 A,
TABLE2 B
WHERE A.ID = B.ID
Just use a correlated subquery with no join:只需使用没有连接的相关子查询:
SELECT A.NAME,
A.AGE,
(SELECT B.SCORE
FROM TABLE2 B
WHERE A.ID = B.ID AND ROWNUM = 1
) as SCORE
FROM TABLE1 A;
Let me note that there is no such thing as "the first row in a table".请注意,没有“表格中的第一行”这样的东西。 Tables represent unordered sets.表代表无序集。 You need a column to specify the ordering.您需要一列来指定排序。 This returns a value from an arbitrary row.这从任意行返回一个值。 It is offered here because you use the same logic in the question.在这里提供它是因为您在问题中使用了相同的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.