简体   繁体   English

SQL连接wordpress表

[英]SQL joining wordpress tables

I am struggling with an SQL query to join 3 tables to return specific results. 我正在努力与SQL查询联接3个表以返回特定的结果。

Anyone familiar with wordpress may be able to assist as I am using word press to power post interactions with the db behind the scenes but am building a custom UI. 熟悉wordpress的任何人都可以提供帮助,因为我正在使用word press在后台与数据库进行后期交互,但正在构建自定义UI。 The three tables are: - 这三个表是:-

posts  (**ID**, post_title, post_content, post_modified_gmt)
term_relationships (**object_id**, term_taxonomy)
terms (**term_id**, name, slug)

I have got as far as.. 我已经到了..

 SELECT posts.post_title FROM posts
 INNER JOIN term_relationships
 ON posts.ID = term_relationships.object_id

Which returns a list of all the post titles that have a matching ID number in term_relationships. 它返回在term_relationships中具有匹配ID号的所有帖子标题的列表。 However term names are in the table 'terms' and the ID's don't match the other two tables. 但是,术语名称在表“ terms”中,而ID与其他两个表不匹配。 In the terms table the term_id refers to the name of the term, eg 在术语表中,term_id指术语的名称,例如

term_id = 2, name = blog

Basically I am trying to achieve a query whereby if I set the term_id = 2 it returns all the rows from the table posts that have the term relationship to blog, meaning the query returns all blog posts and I am completely lost! 基本上,我试图实现一个查询,如果我设置term_id = 2,它将返回与博客具有术语关系的表帖子中的所有行,这意味着查询将返回所有博客帖子,而我完全迷路了!

Can anyone give me a few pointers? 谁能给我一些建议? my mind is boggled. 我的头脑陷入困境。

I managed to achieve the result I was looking for with the following query, but would still appreciate some pointers should anyone stumble upon this and have the inclination :) 通过以下查询,我设法达到了我想要的结果,但是如果有人偶然发现并有这样的倾向,我仍然会喜欢一些指针:)

         SELECT * FROM posts
            INNER JOIN term_relationships
            ON posts.ID = term_relationships.object_id
         WHERE term_relationships.term_taxonomy_id = 2 
            AND posts.post_type = 'post' 
            AND posts.post_status = 'publish'
         ORDER BY posts.post_modified_gmt ASC
         LIMIT 0,5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM