[英]Issue in getting multiple dates in descending order with 2 table and 3 date field join
我想列出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
我得到如下圖所示的輸出
但在上面的輸出日期不是降序排列的,我想像下面的格式輸出
編輯
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
唉, GREATEST()
返回NULL
如果任何參數為NULL
。 因此,一種方法是這樣的:
ORDER BY GREATEST(COALESCE(reg_date, '2000-01-01'),
COALESCE(paymentcancel_date, '2000-01-01'),
COALESCE(payment_date, '2000-01-01')
) desc
我通過下面的代碼解決我的問題
$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.