A
A
acspro2014-12-07 06:12:34
MySQL
acspro, 2014-12-07 06:12:34

How to select datetime from two tables and compare?

Dear gurus)))
How do you like this task.
There are two tables. in these tables there is a modified_id field - this is the manager's id field in another table, there is also a modified field - this is the date of the last change. One of the tables is responsible for depositing funds, the second for spending, these tables contain completely different fields.
It is necessary to select the latest event from the two tables by the modified field return the entire row and fio of the manager, which is contained in the managers table by
modified_id I unworthily lost)
Someone can decide?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
acspro, 2014-12-07
@acspro

На скорую руку решил так - используя php  и кучу запросов наплодив:
        $lastM=$this->db->squery('SELECT m.*, concat(i.`family`,0x20,i.`name`,0x20,i.`middle`) person FROM `mrequests` m LEFT JOIN `managers` as i ON m.`issued`=i.`id` WHERE m.`visible`=true and m.`status`="issued" and m.`currency`="'.$this->db->filter($k).'" order by m.`issued_dt` desc ');
        $lastP=$this->db->squery('SELECT p.`currency`, p.`created`, p.`amount`,  concat(m.`family`,0x20,m.`name`,0x20,m.`middle`) person FROM `prequests` p LEFT JOIN `managers` as m ON p.`manager_id`=m.`id` WHERE p.`visible`=true and p.`currency`="'.$this->db->filter($k).'" order by p.`created` and `type`=true desc ');

        if (($lastM)&&($lastP)){
        $dtM=new datetime($lastM['issued_dt']);
        $dtP=new datetime($lastP['created']);
          if ($dtM<$dtP){
            $currency[$k]['td'].='<td class="text-left">Пользователь '.trim($lastP['person']).' <strong>пополнил кассу на '.$this->ShowMoney($lastP['amount']).' '.$lastP['currency'].'</strong> '.$this->rusdate($dtP->format('Y-m-d')).' г. в '.$dtP->format('H:i:s').'</td>';
          }else{
            $currency[$k]['td'].='<td class="text-left">Пользователь '.trim($lastM['person']).' <strong>вычел из кассы '.$this->ShowMoney($lastM['amount']).' '.$lastM['currency'].'</strong> '.$this->rusdate($dtM->format('Y-m-d')).' г. в '.$dtM->format('H:i:s').'</td>';
          };
        }elseif($dtM){
            $currency[$k]['td'].='<td class="text-left">Пользователь '.trim($lastM['person']).' <strong>вычел из кассы '.$this->ShowMoney($lastM['amount']).' '.$lastM['currency'].'</strong> '.$this->rusdate($dtM->format('Y-m-d')).' г. в '.$dtM->format('H:i:s').'</td>';
          }elseif($dtP){
            $currency[$k]['td'].='<td class="text-left">Пользователь '.trim($lastP['person']).' <strong>пополнил кассу на '.$this->ShowMoney($lastP['amount']).' '.$lastP['currency'].'</strong> '.$this->rusdate($dtP->format('Y-m-d')).' г. в '.$dtP->format('H:i:s').'</td>';
          }else $currency[$k]['td'].='<td class="text-left"></td>';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question