簡體   English   中英

在CONCAT中發現DISTINCT GROUP_CONCAT

[英]DISTINCT GROUP_CONCAT inside CONCAT

我有兩張桌子。

vehicle
+---------+---------+---------+---------------+-------------+---------+-----------+
|   ID    |  number | country | estimate_desc | description | est_date| entry_date|
+---------+---------+---------+---------------+-------------+---------+-----------+

AND vehicle_history

+---------+---------+---------------+-------------+---------+--------------+
|   ID    |  number | estimate_desc | description | est_date| entry_date   |
+---------+---------+---------------+-------------+---------+--------------+

頁面中有一個表格,其中包含車輛表格中的數據。 此外,還有帶有描述歷史記錄的其他列。 每次編輯或添加新行(您只能編輯車輛數據而不是veh_history)描述,估計事件,est_event_date,輸入日期均保存到veh_history表中。 我需要從veh_history表中獲取所有不超過7天的數據。 我所附的代碼是局部有效的,因為例如,如果我僅填寫描述或僅填寫est_event,則無法獲取此數據。 當兩個字段都填寫時,它起作用。

$this->_sql['select'] = ...main table data
$this->_sql['from'] = ...main table

if (filter checkbox is checked) {

$this->_sql['select'].= " , CONCAT(GROUP_CONCAT(DISTINCT est.date_estimate, ' - ' , est.description_estimate SEPARATOR '<BR>'), '<BR>', GROUP_CONCAT(DISTINCT des.date_entry, ' - ' , des.description SEPARATOR '<BR>')) as description_joined";

$this->_sql['from'].= "LEFT JOIN (SELECT * FROM veh_vehicle_history 
                               WHERE date_estimate > DATE_ADD(NOW(), INTERVAL -7 DAY) AND description_estimate <> '' GROUP BY description_estimate ORDER BY date_estimate DESC ) 
                               as est ON est.tractor_unit_id=veh.object_id
                               LEFT JOIN (SELECT * FROM veh_vehicle_history 
                               WHERE date_entry > DATE_ADD(NOW(), INTERVAL -7 DAY) AND description <> '' GROUP BY description ORDER BY date_entry DESC ) 
                               as des ON des.tractor_unit_id=veh.object_id
                              ";
}

我需要過濾不同的值(description,estimate_description),並在一個字符串中顯示這些值。

concat(group_concat(DISTINCT est.date_estimate SEPARATOR '<BR>'),' - ',group_concat(DISTINCT  est.description_estimate))

我希望這能幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM