有一個表如下:
| 元標識 | post_id | 元鍵 | 元值 |
|---|---|---|---|
| 000001 | 1 | 姓名1 | 山姆 |
| 000002 | 1 | 電話 1 | 12345678 |
| 000003 | 1 | 姓名2 | 杰基 |
| 000004 | 1 | 電話 2 | 23456789 |
| 000005 | 2 | 姓名1 | 大衛 |
| 000006 | 2 | 電話 1 | 11111111 |
| 000007 | 2 | 姓名2 | 瑪麗 |
| 000008 | 2 | 電話 2 | 22222222 |
并希望結果是:
| post_id | 姓名1 | 電話 1 | 姓名2 | 電話 2 |
|---|---|---|---|---|
| 1 | 山姆 | 12345678 | 杰基 | 23456789 |
| 2 | 大衛 | 11111111 | 瑪麗 | 22222222 |
(在 WordPress 資料庫上使用 MySQL)
uj5u.com熱心網友回復:
您需要在組中旋轉您的行。
select post_id,
max(case when meta_key = 'name1' then meta_value end) name1,
max(case when meta_key = 'tel1' then meta_value end) tel1,
max(case when meta_key = 'name2' then meta_value end) name2,
max(case when meta_key = 'tel2' then meta_value end) tel2
from tbl
group by post_id
看看這個小提琴。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365701.html
下一篇:使用Mysqlxdevapi
