简体   繁体   English

MYSQL:条件基于相同的表,不同列中的相同值

[英]MYSQL: Condition based on the same table, the same value in a different column

I have a table administ (administrations) containing 4 columns adm_nr , pres_name (president name), year and vice_pres (vice president name). 我有一个表administ (administration),其中包含4列adm_nrpres_name (总统名称), yearvice_pres (副总裁名称)。 I need to select the vice_pres and the amount of times they are featured in the table. 我需要选择vice_pres及其在表中显示的次数。

Here is my SQL 这是我的SQL

SELECT vice_pres, COUNT(vice_pres)
FROM administ
GROUP BY vice_pres

The problem is that there is an extra condition, being I only want to see the results (names & count) for vice presidents who've also been president. 问题是存在一个额外的条件,因为我只想查看同时担任过总裁的副总裁的结果(姓名和人数)。 So there is a value comparison between two columns of the same table, in which the value for vice_pres (the name of the vice president), needs to be equal to at least one value for pres_name in another row but within the same table (in that he's been president at some point). 因此,在同一表的两列之间存在一个值比较,其中Vice_pres(副总裁的名称)的值必须至少等于另一行但在同一表中的pres_name的值(在他曾担任总统)。

I'm thinking there's going to be an self join situation but can't for the life of me figure out how to pen down this condition. 我在想会有一种自我加入的情况,但是我无法终生想出如何降低这种情况的方法。

Thanks in advance! 提前致谢!

Join table with itself to select only those who was president 与表本身联接以仅选择担任总裁的人

SELECT t.vice_pres, COUNT(t.vice_pres)
FROM administ t join administ p on t.vice_pres=p.pres_name
GROUP BY t.vice_pres

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM