当您将两个或多个表合并在一起时,您可以有效地为这些表应用JOIN
条件中指定的过滤器的笛卡尔积。
当您使用过时的隐式JOIN
语法时,这更为明显。
该LEFT JOIN
保证你得到没有少行比最左边的表包含,即最左边的表中的每行至少返回一次。
如果筛选器不是一对一行映射,则仍然可以获取更多行。
你的情况:
SELECT (b.descr || ' - ' || c.descr) description
FROM tbl1 a
LEFT JOIN
tbl2 b
ON b.ACCOUNT = a.ACCOUNT
LEFT JOIN
tbl3 c
ON c.product = a.product
WHERE a.descr50 = ' '
要么acccount
或product
不是b
或c
独特。
对于这些行:
a.account
1
2
3
b.account b.description
1 Account 1
2 Account 2 - old
2 Account 2 - new
,该JOIN
将返回以下:
a.account b.account b.description
1 1 Account 1
2 2 Account 2 - old
2 2 Account 2 - new
3 NULL NULL
,为您提供比任一表中包含多行。
只选择从任一表中的第一匹配的描述中,使用这样的:
SELECT (
SELECT FIRST_VALUE(descr) OVER (ORDER BY descr)
FROM tbl2 b
WHERE b.account = a.account
AND rownum = 1
) || ' - ' ||
(
SELECT FIRST_VALUE(descr) OVER (ORDER BY descr)
FROM tbl3 c
WHERE c.product= a.product
AND rownum = 1
) description
FROM tbl1 a
WHERE a.descr50 = ' '
要进行更新,只是包裹查询分成内联视图:
UPDATE (
SELECT (
SELECT FIRST_VALUE(descr) OVER (ORDER BY descr)
FROM tbl2 b
WHERE b.account = a.account
AND rownum = 1
) || ' - ' ||
(
SELECT FIRST_VALUE(descr) OVER (ORDER BY descr)
FROM tbl3 c
WHERE c.product= a.product
AND rownum = 1
) description
FROM tbl1 a
WHERE a.descr50 = ' '
)
SET descr50 = description
得到的回答在 http://stackoverflow.com/questions/1198183/update-a-table-using-the-fields-of-the-other-two-table-please-help-me-in-this/1198197#1198197 – 2009-07-29 12:03:16
你在用什么'RDBMS'? – Quassnoi 2009-07-29 13:00:34
Prem,看起来你有*四个问题,包括这个问题,*你遇到的问题*。 (1):http://stackoverflow.com/questions/1204701/update-table-with-a-subquery-which-is-returning-more-than-one-row (2):http:// stackoverflow.com/questions/1199733/getting-extra-rows-after-joing-the-3-tables-using-left-join (3):http://stackoverflow.com/questions/1198183/update-a-这张桌子使用这个其他两个字段的表格请帮助我在这 (4):http://stackoverflow.com/questions/1204563/updating-row-with-子查询返回多行 – Liao 2009-07-30 08:06:49