简体   繁体   English

在以2表和3日期字段联接的情况下按降序获取多个日期的问题

[英]Issue in getting multiple dates in descending order with 2 table and 3 date field join

i want to list record with 2 tables and 3 diffrener date field i run below query for that 我想列出2表和3 diffrener日期字段的记录,我在查询下面运行

SELECT id, register.reg_date ,payment_history.payment_date ,paymentcancel_date  
FROM register 
LEFT JOIN payment_history ON payment_history.r_id = register.id 
WHERE user_type = 'Landlord' 
ORDER BY GREATEST(reg_date,paymentcancel_date,payment_date) DESC

and i get output like below image 我得到如下图所示的输出

在此处输入图片说明

but in above output date is not come in decending order i want output like below format 但在上面的输出日期不是降序排列的,我想像下面的格式输出

在此处输入图片说明

EDIT 编辑

SELECT id, register.reg_date ,payment_history.payment_date ,paymentcancel_date  from register  left join  payment_history on payment_history.r_id = register.id where user_type = 'Landlord' ORDER BY COALESCE(reg_date, '2000-01-01') desc,
              COALESCE(paymentcancel_date, '2000-01-01') desc,
              COALESCE(payment_date, '2000-01-01')
              desc

在此处输入图片说明

Alas, GREATEST() returns NULL if any arguments are NULL . 唉, GREATEST()返回NULL如果任何参数为NULL So, one way is something like this: 因此,一种方法是这样的:

ORDER BY GREATEST(COALESCE(reg_date, '2000-01-01'),
                  COALESCE(paymentcancel_date, '2000-01-01'),
                  COALESCE(payment_date, '2000-01-01')
                 ) desc

I solve my issue by below code 我通过下面的代码解决我的问题

$res= $this->property_model->get_all_array("SELECT reg_date as date,f_name,id as id from register order by reg_date desc");
$res1= $this->property_model->get_all_array("SELECT payment_date as date,history_id,r_id as id from payment_history order by payment_date desc");
$data['activity'] = array_merge($res,$res1);

foreach ($activity as $key => $row) {
$volume[$key]  = $row['date'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $activity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM