#!/usr/bin/env python import os import subprocess from itertools import combinations from mysql import connector conn = connector.connect( option_files=os.path.expanduser('~/.my.cnf'), # note that this only works on linux-based OSes like ubuntu and mac. doesn't work on windows. password=subprocess.check_output('grep password ~/.my.cnf | head -1 | sed s/.*=//', shell=True).decode().strip()[1:-1], port='3306', database='db1', ) cursor = conn.cursor() cursor.execute('describe companies') colnames = [c[0] for c in cursor.fetchall()] for c1, c2 in combinations(colnames, 2): cursor.execute("""SELECT DISTINCT COUNT FROM ( SELECT {c1}, count(distinct {c2}) AS count FROM FIRST1a GROUP BY snum ) t1 """.format(c1=c1, c2=c2)) counts = cursor.fetchall() if counts == [(1,)]: print('there is probably FD between {} and {}'.format(c1, c2))