以下是可以满足上述所有条件的 SQL 查询语句:

SELECT * FROM hrbase_mark_contract
WHERE work_id = ':work_id'
ORDER BY id ASC
LIMIT 1
UNION ALL
SELECT * FROM hrbase_mark_contract
WHERE work_id = ':work_id'
GROUP BY company_code
HAVING COUNT(DISTINCT company_code) = 1
ORDER BY id ASC
LIMIT 1
UNION ALL
SELECT * FROM hrbase_mark_contract
WHERE work_id = ':work_id'
AND company_code = ':company_code'
ORDER BY id DESC
LIMIT 1
UNION ALL
SELECT * FROM hrbase_mark_contract
WHERE work_id = ':work_id'
AND company_code <> ':company_code'
AND id > (SELECT MAX(id) FROM hrbase_mark_contract WHERE work_id = ':work_id' AND company_code = ':company_code')
AND id < (SELECT MIN(id) FROM hrbase_mark_contract WHERE work_id = ':work_id' AND company_code = ':company_code')
ORDER BY id DESC
LIMIT 1

请注意将':work_id'和':company_code'替换为实际的参数值。

SQL 查询优化:根据工号和公司代码获取员工合同信息

原文地址: https://www.cveoy.top/t/topic/CpU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录