Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在mysql中根据最近日期选择记录_Php_Mysql - Fatal编程技术网

Php 如何在mysql中根据最近日期选择记录

Php 如何在mysql中根据最近日期选择记录,php,mysql,Php,Mysql,我有如下3张表: fkEmp_id max(t1.date) uid part_hours part_amt first_name employee_id 1 2016-01-04 1 35 120 Ram 001 3 2016-01-08 3 30 150 Jai 003

我有如下3张表:

      fkEmp_id    max(t1.date)   uid  part_hours  part_amt  first_name  employee_id
        1          2016-01-04     1     35          120       Ram         001

        3          2016-01-08     3     30          150       Jai         003
人力资源、环境和工作报酬:

    id   date          fkEmp_id  basic_wage    part_hours   part_amt

     1    04-01-2016    1            4500        35          120
     2    04-01-2016    3            3800        30          150
     3    08-01-2016    3            3200        30          100
人力资源、环境和工作信息:

    id    fkEmp_id

     1      1
     2      3
人力资源管理信息:

   id    employee_id   first_name

    1     001       Ram
    2     002       Lak
    3     003       jai
    4     004       shiva
我想从表1中选择记录,因为列Date值更高

我尝试以下查询:

SELECT t1.fkEmp_id,max(t1.date),max(t1.id) as uid,t1.part_hours,t1.part_amt, t3.first_name, t3.employee_id 
FROM `hr_emp_job_compensation` as t1 
inner join `hr_emp_job_info` as t2 on t1.fkEmp_id = t2.fkEmp_id 
left join `hr_emp_info` as t3 on t3.id = t1.fkEmp_id 
group by t1.fkEmp_id 
但结果如下所示:

      fkEmp_id    max(t1.date)   uid  part_hours  part_amt  first_name  employee_id
        1          2016-01-04     1     35          120       Ram         001

        3          2016-01-08     3     30          150       Jai         003
这里的part_hours和part_amt列是从ID2获取的。如何更改查询

无需为date和id添加MAX。您可以在WHERE子句中处理MAXdate


请查找

请修正您的编辑以包括实际表格。更多详细信息,更多答案。什么表值和什么结果我改变了我的问题。请帮助我在2016-01-08的postSo中添加您的预期结果,您希望结果中的part_hours=30,part_amt=100?但是如果我的表格hr_emp_job_补偿在id 2中具有part_hours和part_amt的最高值,这意味着我想要做什么。我想在hr\U emp\U job\U补偿表的最长日期前得到结果。我更改了我的问题。请帮助meI认为您现在可以删除GROUPBY子句。您还可以将t4上添加的内部联接替换为其中t1.date=SELECT MAXdate FROM hr_emp_job_compensation其中fkEmp_id=t1.fkEmp_id。很抱歉,我很难在注释中添加反向勾号。@unobbre:谢谢您的建议,这很有意义。我更新了答案。