我对这段代码的意图只是能够同时引用3个单独的表,但是能够通过单个列选择特定的行。通常这很容易。但是,其中一些列具有相同的名称,具体取决于表。
如果不加入这些表,如何选择包含一个或多个具有相同名称的列的信息的行?
以下是选择一个具有唯一名称的列的代码(为保护隐私而更改了表/列的名称)。
SELECT *
FROM DBASE.APPLE_ORCHARD,
DBASE.ORANGE_FIELDS,
DBASE.CHERRY_BUSHES
WHERE ORANGES = '50';
现在,我应该做的事情。
SELECT *
FROM DBASE.APPLE_ORCHARD,
DBASE.ORANGE_FIELDS,
DBASE.CHERRY_BUSHES
WHERE WATERING = '100';
(假设每个表都有自己的列名为" WATERING")
我正在使用Oracle SQL Developer来实现此项目
数据样品:
Orange_Fields
COLUMNS -
Oranges
Watering
Insects
Intake
Apple_Orchard
COLUMNS -
Apples
Watering
Insects
CO2
Intake
Cherry_Bushes
COLUMNS -
Cherries
Watering
Insects
Temperature
Intake
我有三个不同列的表。有些列具有相同的名称,而其中一些列则没有。
如何对信息进行排序以接收如下结果:
SELECT *
FROM DBASE.APPLE_ORCHARD,
DBASE.ORANGE_FIELDS,
DBASE.CHERRY_BUSHES
WHERE Intake = '50';
Intake Apples Oranges Cherries Insects
50 32 4
50 12 3
50 52 8
50 16 12
50 13 0
50 84 2
50 18 2
然而,我收到的结果看起来更像是这样。
SELECT *
FROM DBASE.APPLE_ORCHARD,
DBASE.ORANGE_FIELDS,
DBASE.CHERRY_BUSHES
WHERE ORANGES = '50';
ORA-00918: column ambiguously defined
答案 0 :(得分:1)
由于每个表都没有将它们与其他表相关联,因此您必须为每个表创建一个单独的select语句,然后将所有select语句放在一起,我相信这是@jarlh所指的,尝试类似于这样:
SELECT *
FROM (
SELECT intake,
apples,
NULL AS oranges,
NULL AS cherries,
insects
FROM dbase.apple_orchard
UNION ALL
SELECT intake,
NULL AS apples,
oranges,
NULL AS cherries,
insects
FROM dbase.orange_fields
UNION ALL
SELECT intake,
NULL AS apples,
NULL AS oranges,
cherries AS cherries,
insects
FROM dbase.cherry_bushes
) WHERE intake = '50'
ORDER BY insects;