mysql 值作为列名_mysql选择动态行值作为列名,另一列作为值

bd96500e110b49cbb3cd949968f18be7.png

I have a legacy table of user information (that is still in active use) and I cannot change the structure of -

id name value

------------------------------

0 timezone Europe/London

0 language en

0 country 45

0 something x

1 timezone Europe/Paris

1 language fr

1 country 46

timezone/language/country etc are only examples of names, they can be variable/there is no finite list other than unique on rows of that column

I need a MySQL compatible SQL query that would return -

id timezone language country something

---------------------------------------------------

0 Europe/London en 45 x

1 Europe/Paris fr 46

I've looked through various answers on stackoverflow around hacking Pivot table functionality in to MySQL, and similar but none of them seem to match this case of using variable column name alias from unique row values from a column of the same table. Although I have had little sleep so they're all starting to become a bit of a blur, apologies in advance.

Closest I could find would be to use prepared statements https://stackoverflow.com/a/986088/830171 which would first get all possible/unique values from name column and build a query that uses CASE WHEN, and/or multiple sub-SELECT or JOIN on same table queries.

The alternatives I can think of would be to get all rows for that user id and process them in the application itself in a for-loop, or attempt to limit the names to a finite amount and use sub-SELECTs/JOINs. However that second option is not ideal if a new name is added I'd have to revisit this query.

Please tell me I've missed something obvious

解决方案

Unlike some other RDBMS MySQL doesn't have native support for pivoting operations of this sort by design (the developers feel it's more suited to the presentation, rather than database, layer of your application).

If you absolutely must perfom such manipulations within MySQL, building a prepared statement is the way to go—although rather than messing around with CASE, I'd probably just use MySQL's GROUP_CONCAT() function:

SELECT CONCAT(

'SELECT `table`.id', GROUP_CONCAT('

, `t_', REPLACE(name, '`', '``'), '`.value

AS `', REPLACE(name, '`', '``'), '`'

SEPARATOR ''),

' FROM `table` ', GROUP_CONCAT('

LEFT JOIN `table` AS `t_', REPLACE(name, '`', '``'), '`

ON `table`.id = `t_', REPLACE(name, '`', '``'), '`.id

AND `t_', REPLACE(name, '`', '``'), '`.name = ', QUOTE(name)

SEPARATOR ''),

' GROUP BY `table`.id'

) INTO @qry FROM (SELECT DISTINCT name FROM `table`) t;

PREPARE stmt FROM @qry;

EXECUTE stmt;

See it on sqlfiddle.

Note that the result of GROUP_CONCAT() is limited by the group_concat_max_len variable (default of 1024 bytes: unlikely to be relevant here unless you have some extremely long name values).

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值