English 中文(简体)
如果存在一种(另一种)增长,如何获得相关增长?
原标题:How to get a related row if one (another) row exists?
  • 时间:2011-10-19 04:43:25
  •  标签:
  • sql
  • oracle

我知道,这个问题的标题可能没有什么不准确之处,但我可以说得更好。 职业介绍。

I have to fetch 2 different fields, one is always there, the other isn t. That means I m looking at a LEFT JOIN. Good so far.

但是,我所希望的行文是而不是,其存在不确定。

我想做的是:

<<>strong> 和picture,但只有 > 姓名picture_id。 否则,就没有显示任何情况,但我仍然想要知道这些名字,不管他们是否加入。

我知道,这可能是一个小的混淆,但这里有一些cl,因此,我猜想一些人会理解这一点。

我尝试了一些办法,但我只想说一下我想要的。

P.S.: Solutions specified to Oracles are also well.

www.un.org/spanish/ecosoc

EDIT I ve tried some queries but the main problem I found is that, inside the ON clause, I am only able to reference the last table mentioned, in other words: There are four tables from which I m retrieving data, but I can only mention the last (third table) inside the on clause of the LEFT JOIN(which is the 4th table). I ll describe the tables hopefully that ll help. Try not to delve too much on the names, because they are in Portuguese:

表4 我想检索的领域是:。 TB395.dsclaudo and TB397.dscrecomendacao, for a given TB392.nron。 表格如下:

TB392(laudoid,nronip,codlaudo) // laudoid is PK, references TB395

TB395(codlaudo,dsclaudo>> PK

TB398(laud Bot,codrecomendacao)/the pair laudvil,codrecomendacao is PK, reference TB397

TB397(codrecomendacao,dscrecomendacao)/ codrecomendacao is PK

名称相同的领域是外国钥匙。

The problem is that there s no guarantee that, for a given laudoid,there will be one codrecomendacao. But, if there is, I want the dscrecomendacao field returned, that s what I don t know how to do. But even if there isn t a corresponding codrecomendacao for the laudoid, I still want the dsclaudo field, that s why I think a LEFT JOIN applies.

问题回答

象你这样,你的主要增长来源是TB392和TB395的加入;然后,你想要一个外人加入TB398,而当这得到匹配时,你想要研究TB397的相应价值。

I would suggest coding the primary join as one inline view; the join between the two extra tables as a second inline view; and then doing an outer join between them. Something like:

SELECT ... FROM
  (SELECT ... FROM TB392 JOIN TB395 ON ...) join1
  LEFT JOIN
  (SELECT ... FROM TB398 JOIN TB397 ON ...) join2
  ON ...

如果你能够具体说明贵国的表格,哪一栏是表格,哪一栏是表格,哪一栏是它们加入的。 如果没有两个表格或两个表格,则不清楚。 我猜测你有两张表格,因为你谈论的是LEFT JOIN,似乎暗示加入为>,即栏。 因此,你可以使用<条码>NVL2功能,完成你想要的瓦特。 那么,我想一下我从你们的提问中可以做些什么,可能像:

SELECT  T1.name
    ,   NVL2( T2.picture_id, T1.picture, NULL )
FROM    table1 T1
LEFT JOIN
        table2 T2
ON      T1.name = T2.name

如果只有一张桌子,那就更简单。

SELECT  T1.name
    ,   NVL2( T1.picture_id, T1.picture, NULL )
FROM    table1 T1

I think you need:

SELECT ...
FROM
    TB395 
  JOIN               
    TB392
        ON ...
  LEFT JOIN              --- this should be a LEFT JOIN
    TB398
        ON ...
  LEFT JOIN              --- and this as well, so the previous is not cancelled
    TB397
        ON ...

细节可能并不准确:

SELECT 
    a.dsclaudo
  , b.laudoid
  , c.codrecomendacao
  , d.dscrecomendacao
FROM
    TB395 a 
  JOIN
    TB392 b 
        ON b.codlaudo = a.codlaudo
  LEFT JOIN 
    TB398 c
        ON c.laudoid = b.laudoid
  LEFT JOIN 
    TB397 d
        ON d.codrecomendacao = c.codrecomendacao

提出两点意见,然后由您来做。 例如:

Create View view392_395
as
SELECT
t1.laudoid,
t1.nronip,
t1.codlaudo,
t2.dsclaudo
FROM       TB392 t1
INNER JOIN TB395 t2 
        ON t1.codlaudo
         = t2.codlaudo

Create View view398_397
as
SELECT
t1.laudoid,
t1.codrecomendacao,
t2.dscrecomendacao
FROM       TB398 t1
INNER JOIN TB397 t2 
        ON t1.codrecomendacao
         = t2.codrecomendacao

SELECT 
v1.laudoid,
v1.nronip,
v1.codlaudo,
v1.dsclaudo,
v2.codrecomendacao,
v2.dscrecomendacao
FROM            view392_395 v1
LEFT OUTER JOIN view398_397 v2
             ON v1.laudoid
              = v2.laudoid

我认为,意见总是得到利用。 意见是你的朋友。 它们可以简化一些最复杂的问题。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...