用于wordpress的自定义mysql select语句

时间:2022-04-03 04:27:52

I have developed a taxonomy for a site that I've been working on that may be abusing the categorization system of wordpress- posts are categorized both under what topics they refer to (let's say cats, dogs, monkeys) as well as what type of post it is (say, expert, organization, article). So I'd like to find all posts that are about cats and dogs and that are organizations. Something along the lines of IN (cats, dogs) AND IN (organizations)... at least how it makes sense to me, but I can't figure out the right SQL syntax for the task.

我已经开发了一个我一直在研究的网站的分类,可能会滥用wordpress的分类系统 - 帖子分类在他们所指的主题(比如猫,狗,猴子)以及什么类型的发布它(比如专家,组织,文章)。所以我想找到所有关于猫和狗的帖子,这些都是组织。 IN(猫,狗)和IN(组织)的东西......至少对我来说有什么意义,但我无法找到适合该任务的SQL语法。

Based on what I found in this article on wordpress.com, I'm building from the query below... but I'm not sure of the right syntax for how to say 'I want something that belongs to (either category 1 or 2) and (belongs to category 3) (say, cat 1=cats, 2=dogs, 3=organizations).

基于我在wordpress.com上的这篇文章中找到的内容,我正在构建下面的查询...但我不确定如何说'我想要属于的东西(第1类或第1类)的正确语法2)和(属于类别3)(例如,猫1 =猫,2 =狗,3 =组织)。

This is probably really simple and i'll be kicking myself when I get the response.

这可能非常简单,当我收到回复时,我会自己踢。

SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 1,2,3
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->postmeta.meta_value ASC

Thanks!

1 个解决方案

#1


Since the two conditions exist on different rows of the term_taxonomy table, you have to join to that table twice in order to compare them in one row of the result set.

由于这两个条件存在于term_taxonomy表的不同行中,因此必须连接到该表两次才能在结果集的一行中对它们进行比较。

Also I don't think you need to use LEFT JOIN since you're using conditions in the WHERE clause. Outer joins usually perform slower than inner joins.

此外,我认为您不需要使用LEFT JOIN,因为您在WHERE子句中使用条件。外连接通常比内连接执行得慢。

You can use table aliases so you don't have to keep repeating the variable table names.

您可以使用表别名,因此您不必继续重复变量表名称。

So I haven't test this, but the following should be closer to what you need:

所以我没有对此进行测试,但以下内容应该更接近您的需求:

SELECT * FROM $wpdb->posts p
 INNER JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
 INNER JOIN $wpdb->term_relationships r1 ON (p.ID = r1.object_id)
 INNER JOIN $wpdb->term_taxonomy x1
  ON (r1.term_taxonomy_id = x1.term_taxonomy_id)
 INNER JOIN $wpdb->term_relationships r2 ON (p.ID = r2.object_id)
 INNER JOIN $wpdb->term_taxonomy x2
  ON (r2.term_taxonomy_id = x2.term_taxonomy_id)
WHERE x1.term_id IN (1, 2) AND x1.taxonomy = 'category'
 AND x2.term_id = 3 AND x2.taxonomy = 'category'
 AND p.post_status = 'publish'
ORDER BY m.meta_value ASC

#1


Since the two conditions exist on different rows of the term_taxonomy table, you have to join to that table twice in order to compare them in one row of the result set.

由于这两个条件存在于term_taxonomy表的不同行中,因此必须连接到该表两次才能在结果集的一行中对它们进行比较。

Also I don't think you need to use LEFT JOIN since you're using conditions in the WHERE clause. Outer joins usually perform slower than inner joins.

此外,我认为您不需要使用LEFT JOIN,因为您在WHERE子句中使用条件。外连接通常比内连接执行得慢。

You can use table aliases so you don't have to keep repeating the variable table names.

您可以使用表别名,因此您不必继续重复变量表名称。

So I haven't test this, but the following should be closer to what you need:

所以我没有对此进行测试,但以下内容应该更接近您的需求:

SELECT * FROM $wpdb->posts p
 INNER JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
 INNER JOIN $wpdb->term_relationships r1 ON (p.ID = r1.object_id)
 INNER JOIN $wpdb->term_taxonomy x1
  ON (r1.term_taxonomy_id = x1.term_taxonomy_id)
 INNER JOIN $wpdb->term_relationships r2 ON (p.ID = r2.object_id)
 INNER JOIN $wpdb->term_taxonomy x2
  ON (r2.term_taxonomy_id = x2.term_taxonomy_id)
WHERE x1.term_id IN (1, 2) AND x1.taxonomy = 'category'
 AND x2.term_id = 3 AND x2.taxonomy = 'category'
 AND p.post_status = 'publish'
ORDER BY m.meta_value ASC