Sql 需要帮助来连接2个表中的内容吗

Sql 需要帮助来连接2个表中的内容吗,sql,oracle,Sql,Oracle,我有两个表植物和天气,我需要在天气表中唯一的植物物种,在一种天气下开花,植物表中有各自的植物名称 请帮助我构造此条件的sql查询 Plants Table : ===== plant_name; plant_species; seed_year Marcantia1, Bryophyte, 2012 cycas1, gymnosperm, 2013 Marcantia2, Bryophyte, 2016 weather table : ====== plant_species; weat

我有两个表植物和天气,我需要在天气表中唯一的植物物种,在一种天气下开花,植物表中有各自的植物名称

请帮助我构造此条件的sql查询

Plants Table :
=====
plant_name; plant_species; seed_year

Marcantia1, Bryophyte, 2012

cycas1, gymnosperm, 2013

Marcantia2, Bryophyte, 2016

weather table :
======
plant_species; weather_type

Bryophyte, sunny

gymnosperm, rainy

gymnosperm, sunny
例如:在一种天气下开花的唯一植物种类是苔藓植物,它在晴朗的天气下开花,植物表中有两个记录,其中植物种类是苔藓植物,即marcantia1和marcantia2

Required output :
=====
weather_type; plant_name

sunny, marcantia1

sunny, marcantia2

我选择了
LEFT JOIN
,这也将包括不在weather表中的植物。

您需要使用have子句进行分组,该子句具有
count(distinct weather_type)=1

select max(weather_type) as weather_type, plant_name
  from plants p
  join weather w on w.plant_species = p.plant_species
 group by plant_name
having count( distinct weather_type ) = 1;

WEATHER_TYPE    PLANT_NAME
------------    -----------
sunny           Marcantia1
sunny           Marcantia2

这看起来像是一个家庭作业问题。你试过什么?你遇到了什么困难?
select max(weather_type) as weather_type, plant_name
  from plants p
  join weather w on w.plant_species = p.plant_species
 group by plant_name
having count( distinct weather_type ) = 1;

WEATHER_TYPE    PLANT_NAME
------------    -----------
sunny           Marcantia1
sunny           Marcantia2