您的位置:首页技术文章
文章详情页

php - sql查询结果合并的问题

浏览:30日期:2022-06-13 13:43:57

问题描述

问题解答

回答1:

从所提供的信息看,如果只是为了单纯输出文章ID,及对应的属性10086和属性12580的值,那么下面这个简单的SQL就能实现

SELECT art.id, meta1.meta_value AS meta_key10086, meta2.meta_value AS meta_key12580FROM 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’回答2:

你要的是不是 GROUP_CONCAT 这个聚合函数?

回答3:

试试用full join行不。未测试:

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_12580from m1 full join m2 on m1.post_id = m2.post_id and m1.meta_key=’10086’ and m2.meta_key=’12580’

相关文章: