搜尋

首頁  >  問答  >  主體

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

高洛峰高洛峰2784 天前1038

全部回覆(3)我來回復

  • 迷茫

    迷茫2017-04-17 16:54:29

    從所提供的資訊來看,如果只是為了單純輸出文章ID,及對應的屬性10086和屬性12580的值,那麼下面這個簡單的SQL就能實現

    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'

    回覆
    0
  • 巴扎黑

    巴扎黑2017-04-17 16:54:29

    你要的是不是 GROUP_CONCAT 這個聚合函數?

    回覆
    0
  • PHPz

    PHPz2017-04-17 16:54:29

    試試用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_12580
    from m1 full join m2
      on m1.post_id = m2.post_id and
         m1.meta_key='10086' and
         m2.meta_key='12580'

    回覆
    0
  • 取消回覆