mysql多值用逗号分隔符,MySQL:多列值作为1个逗号分隔的字符串?

在不使用预编译语句的情况下,尝试在MySQL触发器中将表的多个列值合并为一个字段,如'fieldvalue1,fieldvalue2,fieldvalue3'。遇到的问题包括无法在触发器内部动态创建查询。解决方案可能涉及在INSERT或UPDATE时预先合并列值到单独的字段。
摘要由CSDN通过智能技术生成

Suppose I have a select query like :

SELECT * FROM tablename

and in the table are columns : field1, field2 and field3

Does anyone know if it's possible to get a resultset with only 1 row with 1 field, with comma separated values of the columns, like this :

"fieldvalue1, fieldvalue2, fieldvalue3"

Problem is that I don't know the column names of the table in advance...

Another problem is that a prepared statement is not the way to go, since all this should be done from within a trigger, and MySQL doesn't allow dynamic cursors/selects inside a trigger.

解决方案

I have done some research and only came as far as GROUP_CONCATenating the column names correctly. But the problem is, that

SELECT (SELECT GROUP_CONCAT( cols.column_name) FROM (SELECT column_name FROM information_schema.columns WHERE table_name='test_table') as cols) FROM test_table

will return one and the same concatenated string containing the column names, once for each table row, instead of evaluating it as the column names for the outer select statement and returning the actual values.

From what I have read in all of the forums discussing this kind of question (and there were many), there is really no way of getting this to work without prepared statements.

I can only think of one other way to do this, and that would be having a dedicated column on each table, where you concatenate the individual column values on INSERT or UPDATE, so you can simply select this one field instead of the full set of fields.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值