php - sql查询结果合并的问题
高洛峰
高洛峰 2017-04-17 16:52:29
0
3
1003
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
迷茫

From the information provided, if you just want to simply output the article ID and the corresponding attribute 10086 and attribute 12580 values, then the following simple SQL can be achieved

SELECT
  art.id,
  meta1.meta_value AS meta_key10086,
  meta2.meta_value AS meta_key12580
FROM wp_posts AS art
  LEFT JOIN wp_postmeta AS meta1
    ON meta1.post_id = art.id AND meta1.meta_key = '10086'
  LEFT JOIN wp_postmeta AS meta2
    ON meta2.post_id = art.id AND meta2.meta_key = '12580'
巴扎黑

Do you want GROUP_CONCAT this aggregate function?

PHPzhong

Try it and see if it worksfull join. Not tested:

with
  m1 as (select * from meta where meta_key = '10086'),
  m2 as (select * from meta where meta_key = '12580')

select case m1.post_id 
  when null then m2.post_id else m1.post_id end as post_id,
  m1.meta_value as meta_key_10086,
  m2.meta_value as meta_key_12580
from m1 full join m2
  on m1.post_id = m2.post_id and
     m1.meta_key='10086' and
     m2.meta_key='12580'
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!