class MainWindow(QMainWindow): def init(self): super().init() # 设置主窗口大小 self.setFixedSize(800, 600) self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint) # 创建主窗口布局 main_layout = QVBoxLayout() central_widget = QWidget() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) # 创建两个竖向按钮 button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.clicked.connect(self.show_query1_result) button_layout.addStretch() button_layout.addWidget(button1) button_layout.addStretch() layout = QHBoxLayout() layout.addStretch() layout.addLayout(button_layout) layout.addStretch() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) # 将按钮布局添加到主窗口布局中 main_layout.addLayout(button_layout) # 创建两个窗口用于展示查询结果 self.query1_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 """ 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 19:53:51 浏览: 168
要按照员工ID查询数据,你需要更改SQL语句。以下是更改后的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
WHERE a.user_id = '{id}'
) T
```
在这个SQL语句中,我们添加了一个WHERE子句来过滤出指定ID的员工数据。在Python代码中,你需要将SQL语句中的{id}替换为你要查询的员工ID。你可以使用QInputDialog来输入员工ID,然后将其传递给SQL语句:
```
from PyQt5.QtWidgets import QInputDialog
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# ...
self.query1_window = QueryResultWindow()
def show_query1_result(self):
# 获取员工ID
id, ok = QInputDialog.getText(self, '输入员工ID', '请输入员工ID:')
if not ok:
return
# 查询数据
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
AND user_id = '{id}'
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
""".format(id=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()
```
这样,你就可以按照员工ID查询工资记录了。
阅读全文