【问题标题】:MySQL: Selecting a row where a column equals the IDMySQL:选择列等于 ID 的行
【发布时间】:2012-12-20 00:34:54
【问题描述】:

这是我的查询:

SELECT ID FROM wp_posts 
WHERE post_type = 'vreb_property'
AND post_status = 'publish' 
ORDER BY post_modified DESC

在这种情况下,我需要修改我的查询以包含 WHERE post_parent = ID ...所以我需要使用正在选择的 ID。

SELECT ID FROM wp_posts WHERE post_type = 'vreb_property' ... and also select records where 'post_parent' = ID

因为我需要抓取post_type“附件”的记录。

我可以请几眼来澄清一下这个请求的有效性吗?谢谢。

【问题讨论】:

  • 您在查询中传递的已知 id?
  • 嗯,比这复杂一点...我在上面编辑了我的问题。

标签: php mysql wordpress


【解决方案1】:

你在找OR吗?

SELECT ID FROM wp_posts 
WHERE (post_type = 'vreb_property'
       AND post_status = 'publish')
   OR (post_type = 'attachment'
       AND post_parent = ID)
ORDER BY post_modified DESC

使用括号您可以根据您的特定需求进行调整,因为这并不完全清楚。

【讨论】:

  • 不幸的是,这只返回了一些结果......条件是post_parent = ID,但需要返回的不仅是匹配post_type = 'vreb_property' AND post_status = 'publish'的行,还有匹配post_type = 'attachment' AND post_parent = ID的行
  • @dcolumbus 这只是对条件的一个小改动,请参阅我的编辑。诀窍在括号中。
  • 不幸的是,这仍然只返回 10 个结果......所以为了澄清,我需要在结果中通过这两种情况。 post_type = 'vreb_property' AND post_status = 'publish' 以及 post_type = 'attachment' AND post_parent = ID
  • @dcolumbus 这正是该查询选择的内容。如果没有给出想要的结果,您应该发布完整的实际代码和未选择的行的示例。
  • 它只给了我vreb_property 行...没有其他行是attachments... 我知道有几十个。
【解决方案2】:

我同意 jeroen 的 sql ,诀窍在括号中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多