# -*- coding: utf-8 -*- import pymysql, io, sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', line_buffering=True) conn = pymysql.connect(host='8.129.84.155', port=3306, user='root', password='Itc@123456', database='crm', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) cur = conn.cursor() cur.execute("SELECT id, username, account, dept_id, enabled, employment_status FROM crm_auth_user WHERE id IN (739564171091247104, 744842318024015872)") for r in cur.fetchall(): print('user:', r) buddy_dept = None cur.execute("SELECT dept_id FROM crm_auth_user WHERE id=744842318024015872") row = cur.fetchone() if row: buddy_dept = row['dept_id'] print('buddy dept_id =', buddy_dept) if buddy_dept: cur.execute("SELECT id, dept_name, leader_user_id FROM sys_dept WHERE id=%s", (buddy_dept,)) for r in cur.fetchall(): print('buddy dept:', r) conn.close()