self.query4_window = QueryResultWindow() # 当日员工工资 def show_query1_result(self): # 查询数据 db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ SELECT *,salary + weight_reward total_salary from ( SELECT a.user_id,user_name,get_time,get_kg,efficiency,CONCAT(ROUND(ROUND(yield_rate,4) * 100,2),'%') yield_rate,ROUND(get_kg * 2 * price,1) salary,CASE WHEN yield_rate > 0.64 and get_kg < 40 THEN kg1_price WHEN yield_rate > 0.64 and get_kg < 50 THEN kg2_price WHEN yield_rate > 0.64 and get_kg >= 50 THEN kg3_price WHEN yield_rate < 0.64 THEN 0 END as weight_reward FROM (SELECT user_id, DATE_FORMAT(get_time,'%Y-%m-%d') get_time, SUM(get_kg) get_kg,round(SUM(get_kg)/(SUM(duration_time)/3600),2) efficiency,AVG(yield_rate) yield_rate FROM em_salary WHERE DATE_FORMAT(get_time,'%Y-%m-%d') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 0 DAY),'%Y-%m-%d') and recycle_kg IS NOT NULL GROUP BY user_id, DATE_FORMAT(get_time,'%Y-%m-%d')) a LEFT JOIN (SELECT user_id,`name` as user_name,price,kg1_price,kg2_price,kg3_price,yield_price FROM `employee_table` CROSS JOIN price_data ) d on a.user_id = d.user_id ) T ORDER BY get_time DESC """ cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return # 清空表格 self.query1_window.table_widget.setRowCount(0) self.query1_window.table_widget.setColumnCount(len(result[0])) self.query1_window.table_widget.setHorizontalHeaderLabels( ["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]) # 添加数据到表格 for row_num, row_data in enumerate(result): self.query1_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) # 显示窗口 self.query1_window.show()怎么改为按ID查询数据
时间: 2023-11-27 09:53:44 浏览: 87
python基础进阶1.6:面向对象之类,对象及__init__(),self相关用法讲解
你可以将原来的 SQL 查询语句中的 `ORDER BY get_time DESC` 改为 `ORDER BY user_id ASC`,然后在查询数据时,将 SQL 语句中的 `DATE_FORMAT(get_time,'%Y-%m-%d') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 0 DAY),'%Y-%m-%d')` 改为 `user_id = '要查询的员工ID'`,这样就可以按照员工ID查询数据了。具体的代码修改可以参考下面的示例:
```python
def show_query1_result(self):
# 获取要查询的员工ID
user_id = self.query1_window.user_id_edit.text().strip()
# 查询数据
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = """
SELECT *,salary + weight_reward total_salary
FROM (
SELECT a.user_id,user_name,get_time,get_kg,efficiency,CONCAT(ROUND(ROUND(yield_rate,4) * 100,2),'%') yield_rate,
ROUND(get_kg * 2 * price,1) salary,
CASE
WHEN yield_rate > 0.64 and get_kg < 40 THEN kg1_price
WHEN yield_rate > 0.64 and get_kg < 50 THEN kg2_price
WHEN yield_rate > 0.64 and get_kg >= 50 THEN kg3_price
WHEN yield_rate < 0.64 THEN 0
END as weight_reward
FROM (
SELECT user_id, DATE_FORMAT(get_time,'%Y-%m-%d') get_time, SUM(get_kg) get_kg,
round(SUM(get_kg)/(SUM(duration_time)/3600),2) efficiency,AVG(yield_rate) yield_rate
FROM em_salary
WHERE user_id = '{}' AND recycle_kg IS NOT NULL
GROUP BY user_id, DATE_FORMAT(get_time,'%Y-%m-%d')
) a
LEFT JOIN (
SELECT user_id,`name` as user_name,price,kg1_price,kg2_price,kg3_price,yield_price
FROM `employee_table` CROSS JOIN price_data
) d ON a.user_id = d.user_id
) T
ORDER BY user_id ASC
""".format(user_id)
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "无工资记录")
return
# 清空表格
self.query1_window.table_widget.setRowCount(0)
self.query1_window.table_widget.setColumnCount(len(result[0]))
self.query1_window.table_widget.setHorizontalHeaderLabels(
["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]
)
# 添加数据到表格
for row_num, row_data in enumerate(result):
self.query1_window.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
# 显示窗口
self.query1_window.show()
```
阅读全文