MYSQL - 如何连接两个查询以省略第二个查询中找到的元素(或者也许有更好的解决方案?)

发布于 2024-11-29 16:31:35 字数 695 浏览 1 评论 0原文

我有一个包含许多自定义字段/元数据的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然 2024-12-06 16:31:36

尝试使用

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() and wp_posts.ID NOT IN (select post_id from wp_postmeta
where wp_postmeta.meta_key = 'Ongoing' AND wp_postmeta.meta_value = 'Yes')

Try to use

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() and wp_posts.ID NOT IN (select post_id from wp_postmeta
where wp_postmeta.meta_key = 'Ongoing' AND wp_postmeta.meta_value = 'Yes')
删除→记忆 2024-12-06 16:31:36

我记得我在那里。您不会想知道我提出的不可扩展且复杂的解决方案。

您只是在寻找几个 paren 和一个 ANDNOT

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
 -- parens make sure everything is grouped correctly and you don't have "and
 -- meta_key - Ongoing and meta_value > NOW()
(wp_postmeta.meta_key = 'Start Date'
 AND wp_postmeta.meta_value > NOW())
-- these conditions are all mutually exclusive, so they could never 
-- simultaneously exist.
AND NOT
(wp_postmeta.meta_key = 'Ongoing'
 AND wp_postmeta.meta_value != 'Yes')

如果您想合并结果,您可以选择使用OR。只需将 AND NOT 替换为 OR 即可,看看会发生什么。

您还可以使用 UNION (尽管当 OR 是一个选项时几乎从未这样做过(我似乎记得可能存在细微的性能差异,但担心这一点是通常是过早的优化)):

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()
-- Distinct vs. ALL is not really relevant here because you can't have overlap
UNION DISTINCT
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'

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 a NOT.

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
 -- parens make sure everything is grouped correctly and you don't have "and
 -- meta_key - Ongoing and meta_value > NOW()
(wp_postmeta.meta_key = 'Start Date'
 AND wp_postmeta.meta_value > NOW())
-- these conditions are all mutually exclusive, so they could never 
-- simultaneously exist.
AND NOT
(wp_postmeta.meta_key = 'Ongoing'
 AND wp_postmeta.meta_value != 'Yes')

IF you wanted to combine the results, you have the option of using OR. Just replace the AND NOT with an OR and see what happens.

You can also use UNION (though that is almost never done when OR is an option (I do seem to remember that there can be subtle performance differences, but worrying about that is often premature optimization)):

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()
-- Distinct vs. ALL is not really relevant here because you can't have overlap
UNION DISTINCT
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'
写给空气的情书 2024-12-06 16:31:35

试试这个:

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()) OR (wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'))

Try this:

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()) OR (wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文