Files
jyls_django/query_caselog.py
2026-02-01 20:22:04 +08:00

61 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pymysql
conn = pymysql.connect(
host='47.108.113.7',
port=3306,
user='jzls',
password='Ls123456',
database='jzls',
charset='utf8'
)
cursor = conn.cursor()
# 查询案件日志总数
cursor.execute('SELECT COUNT(*) FROM business_caselog WHERE is_deleted=0')
total = cursor.fetchone()[0]
print(f'案件日志总数: {total}')
# 查询案件总数
cursor.execute('SELECT COUNT(*) FROM business_case WHERE is_deleted=0')
case_total = cursor.fetchone()[0]
print(f'案件总数: {case_total}')
# 查询每个案件的日志数量
sql = """
SELECT c.id, c.contract_no, COUNT(l.id) as log_count
FROM business_case c
LEFT JOIN business_caselog l ON c.id = l.case_id AND l.is_deleted=0
WHERE c.is_deleted=0
GROUP BY c.id, c.contract_no
ORDER BY c.id
LIMIT 20
"""
cursor.execute(sql)
print('\n案件日志统计前20条:')
print('-' * 60)
print(f'{"案件ID":<10}{"合同编号":<30}{"日志数量":<10}')
print('-' * 60)
for row in cursor.fetchall():
print(f'{row[0]:<10}{(row[1] or ""):<30}{row[2]:<10}')
# 查询最近10条日志
print('\n最近10条案件日志:')
print('-' * 80)
sql2 = """
SELECT l.id, l.case_id, l.content, l.times, l.username
FROM business_caselog l
WHERE l.is_deleted=0
ORDER BY l.id DESC
LIMIT 10
"""
cursor.execute(sql2)
for row in cursor.fetchall():
content = (row[2][:30] + '...') if row[2] and len(row[2]) > 30 else row[2]
print(f'日志ID:{row[0]}, 案件ID:{row[1]}, 时间:{row[3]}, 记录人:{row[4]}')
print(f' 内容: {content}')
cursor.close()
conn.close()