基本上我必须提出一个查询来仅列出所有森林中发现的物种
到目前为止我已经
SELECT Species.Sp_name
FROM Species
LEFT JOIN Tree ON Tree.Tr_species=Species.Sp_name
LEFT JOIN Forest ON Forest.FO_name=tree.Tr_forest
但说实话,我迷路了
物种表
+--------------+------------------+------+--------------------------------+
| Field | Type | Key | Glossary |
+--------------+------------------+------+--------------------------------+
| sp_name | C(10) | PK | Species Name |
| sp_woodtype | C(10) | | Wood Yielded by tree |
| sp_maxht | I | | Max. Height |
+--------------+------------------+------+--------------------------------+
森林表
+--------------+------------------+------+--------------------------------+
| Field | Type | Key | Glossary |
+--------------+------------------+------+--------------------------------+
| Fo_name | C(10) | PK | Forest Name |
| Fo_size | I | | Forest Area |
| Fo_loc | C(10) | | Geographical Area |
| Fo_comp | C(10) | | Forest Owner |
+--------------+------------------+------+--------------------------------+
树表
+--------------+------------------+------+---------------------------------------------+
| Field | Type | Key | Glossary |
+--------------+------------------+------+---------------------------------------------+
| Tr_species | C(10) | FK | (FK of species.sp_name |
| Tr_forest | C(10) | FK | (FK of forest.fo_name |
| Tr_numb | I | PK | Sequence number |
| Tr_planted | Date | | Date of planting |
| Tr_loc | C(10) | | Forest quadrant |
| Tr_parent | I | FK | (FK of tree.numb) procreating tree reference|
+--------------+------------------+------+---------------------------------------------+
测量表
+--------------+------------------+------+---------------------------------------------+
| Field | Type | Key | Glossary |
+--------------+------------------+------+---------------------------------------------+
| Me_trnumb | I | FK | (FK of Tree.tr_numb |
| Me_numb | I | PK | Sequence Number |
| Me_result | I | | Test`s measure |
| Me_date | Date | | Measure taken on |
| Me_type | C(10) | | Type of Measurevv |
+--------------+------------------+------+---------------------------------------------+
(I表示整数,C10表示字符(10)
最佳答案
您可以通过计算每个物种的森林数量并与森林总数进行比较来做到这一点。您可以使用 having
子句来做到这一点:
SELECT s.Sp_name
FROM Species s LEFT JOIN
Tree t
ON t.Tr_species = s.Sp_name
GROUP BY s.Sp_name
HAVING COUNT(DISTINCT t.Tr_Forest) = (SELECT COUNT(*) FROM Forest)
关于mysql - 仅列出与另一个特定属性的所有其他记录链接的特定属性的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23840648/