简体   繁体   English

从子查询中选择多个结果到单行中(作为数组数据类型)

[英]Select multiple results from sub query into single row (as array datatype)

I'm trying to solve a small problem with a SQL query in an oracle database. 我正在尝试解决Oracle数据库中SQL查询的一个小问题。 Let's assume I have these tables: 假设我有这些表:

One table that holds information about cars: 一张包含汽车信息的表:

tblCars
ID    Model    Color
--------------------
1     Volvo    Red
2     BMW      Blue
3     BMW      Green

And another one containing information about drivers: 另一个包含有关驱动程序的信息:

tblDrivers
ID    fID_tblCars    Name
---------------------------
1     1              George
2     1              Mike
3     2              Jason
4     2              Paul
5     2              William
6     3              Steve

Now, let's pretend that to find out the popularity of the cars, I want to create reports that contain the data about the cars and the people that are driving them (which seems a very reasonable thing one would accomplish with a database). 现在,让我们假装要发现汽车的受欢迎程度,我想创建一个报告,其中包含有关汽车及其驾驶人员的数据(这似乎很合理,可以通过数据库来完成)。

This "ReportObject" would have a string for the model, a string for the color and an array (or a list) of strings for the drivers. 该“ ReportObject”将具有用于模型的字符串,用于颜色的字符串和用于驱动程序的字符串数组(或列表)。

Currently, I do this with two queries, in the first I select the cars 目前,我通过两个查询来执行此操作,在第一个查询中,我选择了汽车

SELECT ID, Model, Color FROM tblCars

and create a report object for each result. 并为每个结果创建一个报告对象。

Then, I would take each result and get the drivers for each specific car 然后,我将获取每个结果并获取每个特定汽车的驾驶员

SELECT Name FROM tblDrivers WHERE fID_tblCars = ResultObject.ID

Basically, step one gives me a resulting data set that looks like this: 基本上,第一步提供了一个结果如下所示的数据集:

Result
------------------------------------------
ColumnID        ColumnModel    ColumnColor
Type Integer    Type String    Type String

and now, if I will have more cars in the future, I will have to make a lot of additional queries, one for each row in the resulting table. 现在,如果将来我会有更多的汽车,我将不得不进行很多其他查询,即对结果表中的每一行进行一次查询。

When I try this: 当我尝试这个:

SELECT Model, Color, (SELECT Name FROM tblDrivers WHERE tblDrivers.fID_tblCars = tblCars.ID) as Name FROM tblCars

I get some error message telling me that one result in the row contains multiple elements (which is what I want!). 我收到一些错误消息,告诉我该行中的一个结果包含多个元素(这就是我想要的!)。

I want the result to look like this: 我希望结果看起来像这样:

Result
--------------------------------------------------------
ColumnID        ColumnModel    ColumnColor    ColumnName
Type Integer    Type String    Type String    Type Array

So when I build my report object, I could do something like this: 因此,当我构建报表对象时,我可以执行以下操作:

foreach (var Row in Results)
{
    ReportObject.Model = Row.Model;
    ReportObject.Color = Row.Color;
    foreach (string Driver in Row.Name)
    {
        ReportObject.Drivers.Add(Driver);
    }
}

Am I completely missing my basics here or do I have to split this up in multiple queries? 我是否在这里完全失去了基础知识,还是必须将其分为多个查询?

Thanks! 谢谢!

This works in Oracle. 这在Oracle中有效。 In the SQL Fiddle example I couldn't get the IDENTITY or the PRIMARY KEYS to work when creating the table (never used Oracle SQL before) 在SQL Fiddle示例中,创建表时我无法获得IDENTITY或PRIMARY KEYS的作用(以前从未使用过Oracle SQL)

SELECT  c.id, 
    c.model, 
    c.color,
    LISTAGG(d.name, ',') WITHIN GROUP (ORDER BY d.name) AS "Drivers"
FROM tblCars c
JOIN tblDrivers d
ON c.id = d.fID_TblCars
GROUP BY c.id,
    c.model,
    c.color
ORDER BY c.Id

SQL Fiddle Example SQL小提琴示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM