Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用相同ID的一行中的两个结果_Sql_Oracle - Fatal编程技术网

Sql 使用相同ID的一行中的两个结果

Sql 使用相同ID的一行中的两个结果,sql,oracle,Sql,Oracle,表结构: 水果: | fID | fName | fAmount | SpeciesID | |-----|-------|---------|-----------| | 1 | Apple | 22 | 1 | | 2 | Pear | 4 | 1 | | 3 | Grape | 5 | 1 | | SpeciesID | SpeciesName | SpeciesPrice | |------

表结构: 水果:

| fID | fName | fAmount | SpeciesID |
|-----|-------|---------|-----------|
| 1   | Apple | 22      | 1         |
| 2   | Pear  | 4       | 1         |
| 3   | Grape | 5       | 1         |
| SpeciesID | SpeciesName | SpeciesPrice |
|-----------|-------------|--------------|
| 1         | Fruit       | 100          |
| pID       | fID | ParentIDa | ParentIDb | Colour |
|-----------|-----|-----------|-----------|--------|
| 1         | 1   | 2         | 3         | Red    |
种类:

| fID | fName | fAmount | SpeciesID |
|-----|-------|---------|-----------|
| 1   | Apple | 22      | 1         |
| 2   | Pear  | 4       | 1         |
| 3   | Grape | 5       | 1         |
| SpeciesID | SpeciesName | SpeciesPrice |
|-----------|-------------|--------------|
| 1         | Fruit       | 100          |
| pID       | fID | ParentIDa | ParentIDb | Colour |
|-----------|-----|-----------|-----------|--------|
| 1         | 1   | 2         | 3         | Red    |
家长:

| fID | fName | fAmount | SpeciesID |
|-----|-------|---------|-----------|
| 1   | Apple | 22      | 1         |
| 2   | Pear  | 4       | 1         |
| 3   | Grape | 5       | 1         |
| SpeciesID | SpeciesName | SpeciesPrice |
|-----------|-------------|--------------|
| 1         | Fruit       | 100          |
| pID       | fID | ParentIDa | ParentIDb | Colour |
|-----------|-----|-----------|-----------|--------|
| 1         | 1   | 2         | 3         | Red    |
我想要实现的输出:

| fName | fAmount | SpeciesName | SpeciesPrice | Colour | fID | fName | SpeciesName | SpeciesPrice |
|-------|:-------:|-------------|--------------|--------|-----|-------|-------------|--------------|
| Apple |    22   | Fruit       | 100          | Red    | 2   | Pear  | Fruit       | 100          |
| Apple |    22   | Fruit       | 100          | Red    | 3   | Grape | Fruit       | 100          |
前5列用于子表,颜色取自父表。 现在最重要的部分是,我需要使用ParentIDa(第一个结果)和ParentIDb(第二个结果)将其添加到使用水果表和fID的列中,然后再为parents表连接物种

简而言之,有些水果有父对象,列在父表中,我想先显示子对象信息,然后显示Parent1信息,第二个结果应该是相同的child info+Parent2信息

到目前为止,我得到的是:

SELECT  fName,fAmount,SpeciesName,SpeciesPrice,Colour,fID,sub.*
FROM Fruit f 
INNER JOIN Parent pa ON (f.fID = pa.fID) 
INNER JOIN Species s ON (a.SpeciesID = s.SpeciesID)
INNER JOIN (
    SELECT fID,fName,SpeciesID 
    FROM Fruit 
    WHERE pa.ParentIDa = f.fID) sub;

我只是无法让内部查询工作,我需要以某种方式使用输出的主fID来将其用于子查询。

这不是您需要的吗

SELECT f.fName,f.fAmount,s.SpeciesName,s.SpeciesPrice, pa.Colour, f.fID, f.fName, s.SpeciesName, s.SpeciesPrice 
FROM Fruit f 
INNER JOIN Parent pa ON f.fID = pa.fID 
INNER JOIN Species s ON f.SpeciesID = s.SpeciesID
WHERE pa.ParentIDa = f.fID


认为问题出在子选择中,无法引用外部查询。尝试不执行子选择,而只是在最后一条语句中使用FROURT连接,并为此表分配另一个别名

INNER JOIN Fruit f2 WHERE pa.ParentIDa = f2.avatarID

我用sqlFiddle来回答你的问题,请看编辑

您的确切输出是:

SELECT f.fName,f.fAmount,s.SpeciesName,s.SpeciesPrice, p.Colour, f.fID, f.fName, s.SpeciesName, s.SpeciesPrice 
FROM Fruit f 
INNER JOIN Species s ON f.SpeciesID = s.SpeciesID
INNER JOIN Parent p ON f.fID != p.fID 
如果你想看到相同的id使用

INNER JOIN Parent p ON f.fID = p.fID
使用时!=操作员输出为:

Pear    4   Fruit   100 Red 2   Pear    Fruit   100
Grape   5   Fruit   100 Red 3   Grape   Fruit   100
Apple   22  Fruit   100 Red 1   Apple   Fruit   100
如果使用=运算符,则输出为:

Pear    4   Fruit   100 Red 2   Pear    Fruit   100
Grape   5   Fruit   100 Red 3   Grape   Fruit   100
Apple   22  Fruit   100 Red 1   Apple   Fruit   100
编辑:ParentIDa和ParentIDb关节

SELECT f.fName,f.fAmount,s.SpeciesName,s.SpeciesPrice, p.Colour, f.fID, f.fName, s.SpeciesName, s.SpeciesPrice 
FROM Fruit f 
INNER JOIN Parent p ON (p.ParentIDa = f.fID or p.ParentIDb = f.fID)
INNER JOIN Species s ON f.SpeciesID = s.SpeciesID;

谢谢各位,现在我知道答案了:

SELECT f.fName,f.fAmount,s.SpeciesName,s.SpeciesPrice, p.Colour, f2.fID, f2.fName, f2.SpeciesID, s2.SpeciesName, s2.SpeciesPrice
FROM Fruit f 
INNER JOIN Species s ON f.SpeciesID = s.SpeciesID
INNER JOIN Parent p ON f.fID = p.fID
INNER JOIN Fruit f2 ON p.ParentIDa = f2.fID OR p.ParentIDb = f2.fID
INNER JOIN Species s2 ON f2.SpeciesID = s2.SpeciesID;

请参见

您从哪里获得的
avatarID
?对不起,应该是f.fID。修好了,现在检查一下。更新了我的答案。@ZenkTM我用sqlFiddle来回答您的问题,输出结果就是您所描述的。。你需要什么?或者有什么问题?这不会在相同的结果中显示父级,pa.ParentIDa=f.fid表中会有更多记录,因此我不能使用!=运算符,我需要使用parentIDa=fID或parentIDb=fID。@ZenkTM您是否检查了我的链接还有f.fID=p.fID解决方案是的,parentID也没有联接。SQLFIDLE总是最适合此类任务,但我看到您偷走了所有创建表代码:)