MySql - 将多列连接/合并为带有硬编码标题的一列 [英] MySql - Concatenate/merge multiple columns into one column with hard codded titles

查看:40
本文介绍了MySql - 将多列连接/合并为带有硬编码标题的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该将多列文本迁移到一列(在另一个数据库中).现在我知道这听起来很复杂,但我会尽力解释它.mysql 数据库中有一个名为products_desriptions"的表.在该表中,我们有如下列:descriptionDosage、descriptionAction、descriptionIndications 等.我想要做的是将这些列合并到一个名为productDescription"的列中.这是我所做的一个例子:

i should migrate multiple columns of text into one column (in another database). Now i know that it sounds complicated, but i will try my best to explain it. There is a table in mysql database called "products_desriptions". In that table we have columns like: descriptionDosage, descriptionAction, descriptionIndications and so on. What i want to do is to merge these columns into one column called "productDescription". Here is an example of what i have done:

SELECT 
   CONCAT_WS('\ntest', action, Indications, staff, dosage) AS productDescription)
FROM 
   products_descriptions
WHERE 
   product_id = 123

所以列合并成功.问题是,对于每一列,首页都有一个标题,它没有被存储到相应的列中.例如,在剂量"列中,我们有这样的文本:2xday, 14xweek"等等.在首页上,您可以看到如下内容:剂量:2xday,14xweek"等等.因此,对于每一列,都有一个像这样的硬编码标题,我不知道如何获得.我的意思是我想要做的是:合并列并分别为每一列我想要相应文本的标题.我正在研究 php,所以也许不可能只通过 mysql 来完成.也许我必须用 php 来做,但有人知道我到底应该做什么吗?

So the the columns are being merged successfully. The problem is that for each column there is a title in the frontpage, which is not being stored into the respective column. For example in the "dosage" column we have text like: "2xday, 14xweek" and so on. And on the frontpage what you can see is something like: "DOSAGE: 2xday, 14xweek" and so on. So for each column there is a hard codded title like this one, which i don't know how to get. I mean what i want to do is: to merge columns and respectively for each column i want a title to the respective text. I am working on php, so maybe it is not possible to be done only by mysql. Maybe i have to do it with php, but has anyone got any idea what exactly should i do?

推荐答案

可以使用concat函数.

SELECT 
   CONCAT('\ntest', action, '\nIndictions:', Indications,'\nstaff:', staff,'\ndosage:', dosage) AS productDescription
FROM 
   products_descriptions
WHERE 
   product_id = 123

这篇关于MySql - 将多列连接/合并为带有硬编码标题的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆