SELECT
post_id,meta_key,meta_value
FROM
wp_postmeta
WHERE
post_id > 3185 AND
meta_key = '_shipping_first_name' OR
post_id > 3185 AND
meta_key = '_shipping_last_name' OR
post_id > 3185 AND
meta_key = '_shipping_postcode'
ORDER BY post_id
post_id meta_key meta_value
3186 _shipping_first_name Joe
3186 _shipping_last_name Svelnys
3186 _shipping_postcode 06040
这是输出数据。我正在尝试将其格式化为
Order FName LName Zip
3186 Joe Svelnys 06040
有什么想法吗?我真的很感激。
这是一个帮助的Sqlfiddle http://sqlfiddle.com/#!2/83e232
答案 0 :(得分:1)
select wp1.post_id, wp2.meta_value as FName, wp3.meta_value as LName, wp1.meta_value as Zip
from wp_postmeta wp1
left join wp_postmeta wp2
on wp1.post_id = wp2.post_id
and wp2.meta_key = '_shipping_first_name'
left join wp_postmeta wp3
on wp1.post_id = wp3.post_id
and wp3.meta_key = '_shipping_last_name'
where wp1.post_id = 3185
and wp1.meta_key = '_shipping_postcode';
这样的东西?
答案 1 :(得分:1)
select post_id as OrderNo,
(select meta_value from wp_postmeta where post_id = w.post_id and meta_key = '_shipping_first_name') as FName,
(select meta_value from wp_postmeta where post_id = w.post_id and meta_key = '_shipping_last_name') as LName,
(select meta_value from wp_postmeta where post_id = w.post_id and meta_key = '_shipping_postcode') as Zip
from wp_postmeta w where w.post_id > 3185 group by post_id
答案 2 :(得分:0)
标准解决方案看起来像这样,但是类似于J33nn的解决方案版本应该也能正常工作 - 甚至可能更快!
SELECT post_id
, MAX(CASE WHEN meta_key = '_shipping_first_name' THEN meta_value END) fname
, MAX(CASE WHEN meta_key = '_shipping_last_name' THEN meta_value END) lname
, MAX(CASE WHEN meta_key = '_shipping_postcode' THEN meta_value END) zip
FROM wp_postmeta
WHERE post_id > 3185
GROUP
BY post_id;