显示重复记录

时间:2010-08-05 17:11:46

标签: php mysql

我有一个名为problemTBL的表,其中包含以下字段

problem_id Autoincrement,Pk
passenger_id, accept duplicate values
problem_name, problem description 

这些是样本记录:

Problem_id passenger_id    problem_name
1              22         NO Air condition 
2              22         Dirty Seats
3              24         Driver Not helped
4              22         Old Age Car
5              23         Old Age Driver
6              22         Very speed driving

现在我想要检索像

这样的记录
["NO Air condition","Dirty Seats","Old Age Car","Very speed driving","","","","","","","","","","",""] passenger_id 22

["Driver Not helped","","","","","","","","","","","","","",""] passenger_id 24

OR

passenger_id    problem_name
22              NO Air condition |=| Dirty Seats |=| Very speed driving |=|Old Age Car
24              Driver Not helped
23              Old Age Driver

是否可以使用查询?

嗨,我正在做迁移,

目前我现在每个问题都存在,现在我正在进行迁移, 我想将特定客户的所有问题保留在一行中,

这是一个客户可以提高合规至少15(对于投诉我们有单独的表格,我们在那里提出一些问题,他们只是勾选并提交..)

所以我们已经明确确定只有15个问题,

一个客户一行,

那是现在最新的合规表格有15个投诉,如果他们选择了,那么那个问题会检查否则空的val会被插入,

提交表单时,我只是将所有值发布到json_encoded中,然后将此json插入表中...

明白我的意思......

对于未来的投诉,我做得很好,

但是我想迁移过去的兼容性,所以我在这里发布了问题,。,,

2 个答案:

答案 0 :(得分:1)

您可以使用group_concat()聚合多行的文本字段。

SELECT
        `passenger_id`, 
        GROUP_CONCAT(`problem_name` SEPARATOR ' |=| ')
FROM
    `problemTBL`
GROUP BY 
    `passenger_id`

此外,您可能希望增加group_concat_max_len变量。

在SELECT查询之前: SET SESSION group_concat_max_len = max_bytes

答案 1 :(得分:0)

在这里阅读有关交叉表(即数据透视表)的信息:

http://onlamp.com/pub/a/onlamp/2003/12/04/crosstabs.html