[英]How do I get a result set based on instances from the same table?
I have a database with the table "Characters" and the table "Planets" from the star wars movies. 我有一个数据库,其中包含《星球大战》电影中的“角色”表和“行星”表。 I need to find out the names of the characters that have the same affiliation as their homeplanet.
我需要找出与本位机有相同隶属关系的角色的名称。 The data is in the following images :
数据在以下图像中:
Characters 人物
Planets 行星
I tried this but it doesn't work 我试过了但是没用
SELECT character_name
FROM characters c1
INNER JOIN characters c2
ON c1.affiliation = c2.affiliation AND c1.homeworld = c2.homeworld
GROUP BY character_name;
You need to join characters
table to planets
table by planet name, then filter rows using same affiliations: 您需要按行星名称将
characters
表连接到planets
表,然后使用相同的从属关系过滤行:
SELECT character_name
FROM characters INNER JOIN planets ON characters.homeworld = planets.planet_name
WHERE characters.affiliation = planets.affiliation;
You need to join the two tables and then constrain by the affiliation. 您需要加入两个表,然后通过从属关系进行约束。 You can add this constraint in the join clause or the where clause, so both of these work:
您可以在join子句或where子句中添加此约束,因此这两个工作都可以:
SELECT character_name
FROM characters c
INNER JOIN planets p ON
c.homeworld = p.homeworld
AND c.affiliation = p.affiliation
or 要么
SELECT character_name
FROM characters c
INNER JOIN planets p ON
c.homeworld = p.homeworld
WHERE c.affiliation = p.affiliation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.