MYSQL - 如何连接两个查询以省略第二个查询中找到的元素(或者也许有更好的解决方案?)
我有一个包含许多自定义字段/元数据的 WordPress 数据库。
---------
wp_postmeta
---------
post_id
meta_key
meta_value
我想查找meta_key 为“开始日期”且值为“NOW()”的所有帖子,但不是meta_key 为“正在进行”且值为“是”的帖子。
我对如何加入查询有点困惑:
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()
with
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'
以及可能的其他查询...我认为让我失望的是我无法在语句中添加另一个 WHERE 子句?我想我只需要一个正确方向的产品,因为我缺少一些基本的 mysql 理解。
提前非常感谢!
I have a wordpress database with a number of custom fields/metadata.
---------
wp_postmeta
---------
post_id
meta_key
meta_value
I want to find all the posts with a meta_key of 'Start Date' and a value of 'NOW()', but not the posts that have the meta_key 'Ongoing' and value of 'Yes'.
I'm a bit confused of how to join the queries:
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()
with
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'
And potentially other queries... I think what is throwing me off is that I can't add another WHERE clause to the statement? I think I just need a prod in the right direction because I am missing some basic mysql understanding.
Thank you so much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
Try to use
我记得我在那里。您不会想知道我提出的不可扩展且复杂的解决方案。
您只是在寻找几个 paren 和一个
AND
与NOT
。如果您想合并结果,您可以选择使用
OR
。只需将AND NOT
替换为OR
即可,看看会发生什么。您还可以使用
UNION
(尽管当OR
是一个选项时几乎从未这样做过(我似乎记得可能存在细微的性能差异,但担心这一点是通常是过早的优化)):I remember being there. You don't want to know the unscalable, convoluted solution I came up with.
You're just looking for a couple of paren's and an
AND
with aNOT
.IF you wanted to combine the results, you have the option of using
OR
. Just replace theAND NOT
with anOR
and see what happens.You can also use
UNION
(though that is almost never done whenOR
is an option (I do seem to remember that there can be subtle performance differences, but worrying about that is often premature optimization)):试试这个:
Try this: