我在查询工作期间遇到了困难 我有一个看起来像这样的客户数据库
客户
字段
FieldsContent
最后我想制作一张看起来像这样的桌子
希望你能帮我找到怎么做,
我用php编写了它,你可以看到:
$this->db->query('SET SESSION group_concat_max_len=10000000');
$q = $this->db->select("GROUP_CONCAT(fieldsContent.Content SEPARATOR '|*|') AS Content,
GROUP_CONCAT(fieldsContent.FieldID SEPARATOR '|*|') AS FieldID,
customers.Created,
customers.ID",false)
->from('customers')
->join('fieldsContent','fieldsContent.TypeID = customers.ID','left outer')
->where(array('fieldsContent.TypeName' => 'customers',
'customers.ID' => $id))
->group_by('fieldsContent.TypeID')
->order_by('customers.Created','DESC')
->get();
foreach($q->result() as $d){
$field = explode('|*|',$d->FieldID);
$content = explode('|*|',$d->Content);
foreach($field as $k => $FieldID){
$dd[$FieldID] = $content[$k];
}
}
但我只需要sql而不使用php
答案 0 :(得分:0)
可能是以下情况?
SET SESSION group_concat_max_len=10000000;
CREATE VIEW <view_name> AS
(
SELECT group_concat(fieldscontent.content separator '|*|') AS content,
group_concat(fieldscontent.fieldid separator '|*|') AS fieldid,
customers.created
FROM customers
LEFT JOIN fieldscontent
ON fieldscontent.typeid = customers.id
GROUP BY fieldscontent.typeid
ORDER BY customers.created DESC
)
然后你可以过滤视图!