Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何压缩像这样的柱?_Mysql - Fatal编程技术网

Mysql 如何压缩像这样的柱?

Mysql 如何压缩像这样的柱?,mysql,Mysql,我试着找到这样的东西,但是没有用 这是关于一个客户管理系统的表系统。特别是,我需要为每个客户创建注释历史记录 因此,我有一个表'customers',其中包含列customers.customer\u ID,customers.lastname,customers.firstname,customers.postal\u code,customers.city和customers.street 和另一个表“notes”,其中列为notes.notes\u ID,notes.customer\u

我试着找到这样的东西,但是没有用

这是关于一个客户管理系统的表系统。特别是,我需要为每个客户创建注释历史记录

因此,我有一个表'customers',其中包含列
customers.customer\u ID
customers.lastname
customers.firstname
customers.postal\u code
customers.city
customers.street

和另一个表“notes”,其中列为
notes.notes\u ID
notes.customer\u ID
notes.subject
notes.description
notes.entered\u on

现在我需要创建第三个表搜索,它浓缩了上面的大部分信息。它有search.contact_ID、search.name、search.address和search.history表。这应该是这样的:

contacts:
contact_ID  | lastname  | firstname | ...
------------+-----------+-----------+-----
1           | Doe       | John      | ...
2           | Dane      | Jane      | ...

note:
note_ID | contact_ID    | subject               | description           | entered_on
--------+---------------+-----------------------+-----------------------+----------------
1       | 1             | call received         | John Doe called us to | 2014-05-03
        |               |                       | ask for an offer      |
2       | 1             | offer made            | We called John Doe to | 2014-06-03
        |               |                       | submit our offer      |
3       | 2             | advertisement call    | We called Jane Dane to| 2014-06-03
        |               |                       | inform her of our     |
        |               |                       | latest offer          |
4       | 1             | offer accepted        | John Doe called to    | 2014-08-03
        |               |                       | accept our offer      |

search:
contact_ID  |   name        |   address                       | history
------------+---------------+---------------------------------+-------------------
1           | Doe, John     | 55 Main Street, 12345 Oldtown   | 'On 2014-08-03 offer accepted: John Doe accepted our offer.  
            |               |                                 | On 2014-06-03 offer made: We called John Doe to submit our offer. 
            |               |                                 | On 2014-05-03 call received: John Doe called us to ask for an offer.'
2           | Dane, Jane    | 111 Wall Street, 67890 Newtown  | 'On 2014-06-03 advertisement call: We called Jane Dane to submit our offer.' 
虽然我可以处理其余的大部分,但我不知道如何生成历史信息。我的想法如下

WHILE 
    customers.customer_ID = note.customer_ID 
    AND 
    note.entered_on = GREATEST(note.entered_on)
DO 
    SET customers.note_history = CONCAT_WS(' | ', CONCAT_WS(': ',note.subject,note.description), customers.note_history);

但这并不一定是按时间顺序的。另外,我如何将其转换为与用于创建表其余部分的
SELECT into
兼容的语句?

听起来像是Group By以及
Group_CONCAT

CREATE TABLE search (PRIMARY KEY(contact_ID))
  SELECT contact_ID, CONCAT(lastname,', ',firstname) AS name, address, 
    GROUP_CONCAT(CONCAT('On ',entered_on,' ',subject,': ',description) 
        ORDER BY note_ID SEPARATOR "\n") AS history
  FROM contacts LEFT JOIN note USING (contact_ID)
  GROUP BY contact_ID

如果不想使用
创建表。。选择…
,可以先创建(或截断!)表,然后使用
插入到。。。改为选择…

听起来你想在MySQL中做一些应该在你的应用程序中完成的事情。仔细检查一下,如果你没有地址列,它就需要合成。但是name的例子显示了使用CONCAT函数来完成这项工作。谢谢,当使用ORDER BY entered_on来确保分组的时间顺序时,该函数起到了作用。