MySQL Join Same Table

前端 未结 3 461
离开以前
离开以前 2020-12-29 07:49

I have the table \'meta_data\' with the following fields:

  • id
  • post_id
  • meta_key
  • meta_value

I\'d like to loop throug

相关标签:
3条回答
  • 2020-12-29 08:17

    To achieve this you should use the LEFT OUTER JOIN operation joining the same table.

    SELECT a.*
    FROM meta_data a
    LEFT OUTER JOIN meta_data b ON a.post_id = b.post_id AND b.meta_value = 'def'
    WHERE 
    a.meta_value = 'abc'
    AND b.post_id IS null
    
    0 讨论(0)
  • 2020-12-29 08:34

    Make an outer (left) join to itself, filtering on those records that don't match by looking for rows with a null id in the joined table:

    select t1.* 
    from meta_data t1
    left join meta_data t2 on t2.post_id = t1.post_id and t2.meta_key='def' 
    where t1.meta_key='abc'
    and t2.id is null
    
    0 讨论(0)
  • 2020-12-29 08:36

    select md1.*

    from meta_data md1

    left join meta_data md2 on md1.post_id = md2.post_id and md2.meta_key='def'

    where md1.meta_key='abc'

    and md2.post_id is null

    0 讨论(0)
提交回复
热议问题