我是新發布,但社區已成爲我迄今爲止項目中的最佳資源。 我是一個愚蠢的/虛擬的Mysql「想成爲」,我正處在一個讓我發瘋的項目中。如何合併具有相同id的多個行mysql
我有一個wordpress plugin buddypress的表,它將meta_key和meta_values配對,以創建類似於分類的東西。我的職責是使用這些配對值來實現高級組搜索。這裏是原來的表:
--------------------------------------------
id | group_id | meta_key | meta_value
--------------------------------------------
1 | 1 | time-zone | Kwajalein
2 | 1 | playstyle | hardcore
3 | 1 | recruiting-status | Open
4 | 1 | ilvl | 115
5 | 1 | main-raid | Final Coil of Bahamut
6 | 1 | voicechat | fc.teamspeak3.com
等....
使用視圖我設法創造begginers一個更友好的檢索表:
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 | | |
1 |Kwajalein | |
1 | | hardcore |
1 | | |
1 | | | Final Coil of Bahamut
1 | | |
這裏是查看代碼:
SELECT distinct
group_id AS 'gid',
IF(meta_key='recruiting-status',meta_value,'') AS 'Recruitment',
IF(meta_key='server',meta_value,'') AS 'server',
IF(meta_key='time-zone',meta_value,'') AS 'tzone',
IF(meta_key='main-raid',meta_value,'') AS 'raid',
IF(meta_key='raid-days',meta_value,'') AS 'days',
IF(meta_key='playstyle',meta_value,'') AS 'playstyle',
IF(meta_key='raid-progression',meta_value,'') AS 'progression',
IF(meta_key='raid-time',meta_value,'') AS 'time',
IF(meta_key='tanker-spot',meta_value,'') AS 'tank',
IF(meta_key='healer-spot',meta_value,'') AS 'healer',
IF(meta_key='melee-dps-spot',meta_value,'') AS 'melee',
IF(meta_key='ranged-dps-spot',meta_value,'') AS 'ranged',
IF(meta_key='magic-dps-spot',meta_value,'') AS 'magic',
IF(meta_key='ilvl',meta_value,'') AS 'ilvl',
IF(meta_key='voicechat',meta_value,'') AS 'voice',
IF(meta_key='voicechatpass',meta_value,'') AS 'voicep',
FROM wpstatic_bp_groups_groupmeta
的一點是,我需要合併該結果(圖),以便所有的GROUP_ID = 1或2或3等站在一個單列,像這樣:
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 |Kwajalein | hardcore | Final Coil of Bahamut
2 |SaoPaulo | regular | Second Coil of Bahamut
等
任何人都可以幫助我嗎?
這是類似於此http://stackoverflow.com/questions/16568228/how-to-transpose-mysql-table-rows-into-columns但我認爲你可以添加到年底你認爲'GROUP BY gid'可能嗎? – 2015-02-05 20:21:01
我試了一下,除了gid列以外沒有任何數據。 – 2015-02-06 11:08:20