CONCAT中的DISTINCT GROUP_CONCAT

时间:2012-12-27 08:32:13

标签: mysql distinct group-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)描述,estimated_event,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)并在一个字符串中显示这些值。

1 个答案:

答案 0 :(得分:0)

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

我希望这会对你有所帮助。