加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
work1.php 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
司空见惯 提交于 2024-06-25 09:58 . 2
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "liu";
// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 查询每个部门的员工数量和性别统计
$sql = "SELECT d.department,COUNT(e.id) AS total_employees,
SUM(CASE WHEN e.gender = '0' THEN 1 ELSE 0 END) AS male_count,
SUM(CASE WHEN e.gender = '1' THEN 1 ELSE 0 END) AS female_count
FROM
employees e
LEFT JOIN
department d ON e.depart_id = d.id
GROUP BY
d.department";
$result = $conn->query($sql);
// 准备以JSON格式输出的数据
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = [
'department' => $row['department'],
'totalEmployees' => (int)$row['total_employees'],
'maleCount' => (int)$row['male_count'],
'femaleCount' => (int)$row['female_count']
];
}
// 查询某员工的工资数据
$employeeId = 1; // 示例员工ID
$sqlSalary = "SELECT date, salary FROM salaryr WHERE employee_id = $employeeId ORDER BY date";
$resultSalary = $conn->query($sqlSalary);
// 准备以JSON格式输出的工资数据
$salaryData = [];
while ($row = $resultSalary->fetch_assoc()) {
$salaryData[] = [
'date' => $row['date'],
'salary' => (float)$row['salary']
];
}
// 关闭数据库连接
$conn->close();
// 将数据编码为JSON格式,用于AJAX响应
header('Content-Type: application/json');
echo json_encode(['departmentData' => $data, 'salaryData' => $salaryData]);
?>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化