MySQL联接相同的表 [英] MySQL Join Same Table

查看:85
本文介绍了MySQL联接相同的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有以下字段的表"meta_data":

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

  • id
  • post_id
  • meta_key
  • meta_value

我想遍历并显示EACH帖子(post_id)的列表,该列表具有meta_key='abc'的条目,而 not meta_key='def'

I'd like to loop through and display a list of EACH post (post_id) that has an entry for meta_key='abc' but not one for meta_key='def'

基本上,每个具有meta_key='abc'条目 的帖子都应具有meta_key='def'条目.我想生成列表,以便可以添加缺少的meta_key='def'条目.

Basically, every post that has a meta_key='abc' entry should have a meta_key='def' entry. I want to generate the list so I can add the missing meta_key='def' entries.

推荐答案

要实现此目的,您应该使用

To achive 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.id IS null

这篇关于MySQL联接相同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆