mysql - 使用 JOIN 在 Wordpress 中进行高级 MySQL 查询

标签 mysql sql wordpress join

我是 MySQL 的新手,我正在尝试编写一个非常高级的查询。但是,嘿!边做边学 & Stackoverflow!

我可能可以在不太高级的查询中获取所有数据并使用 PHP 对数据进行排序。但我认为它可以直接在查询中完成。

下面是我的代码。如果您不明白,请提出问题,我会尽力解释得更好。如果您发现任何错误,请帮助我更正代码。

该代码将用于在我的 wordpress 页面上显示不同的字段。不同的字段将有不同的类别,例如。 sidebar-blog, sidebar-page, highlight-blog, highlight-page.它几乎可以像普通帖子一样工作。

这是 Wordpress 的数据库结构:

问题:

我应该如何加入表:wp_posts、wp_term_relationships、wp_term_taxonomy、wp_terms 和 wp_postmeta?

进行高级查询是好习惯还是应该使用 PHP 来处理 if/else 函数?

<?php

$id = $post->ID; // Gets the ID of current page

$query = "
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug        # Data from two different tables

FROM wp_posts

# Cant figure out how to join the tables
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id

INNER JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id

INNER JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id

INNER JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id




WHERE
wp_posts.post_type = 'my-own-post-type'         # Only get specific post-type
AND
wp_posts.post_status = 'publish'                # Only get published posts


AND

# START - Only get categories specified here
(
wp_terms.slug = 'category-1'
OR
wp_terms.slug = 'category-2'
OR
wp_terms.slug = 'category-3'
OR
wp_terms.slug = 'category-4'
OR
wp_terms.slug = 'category-5'
)
# END - Only get categories specified here


AND

# I want to be able to include or exclude specific pages, even if category is the right one
# Exlude = Don't get data if current page ID is found (ID of current page will be checked in meta_value using %,$id,%)
# Include means: If include value is specifyed, then get data ONLY if ID is found (ID of current page will be checked in meta_value using %,$id,%)
# If not exclude and include are set, it should get the data

# START - Include exclude
(
# exclude is set, so check if page id match anywhere. If it IS found it it should not get the data, otherwise YES
wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '%,$id,%'
OR
# include is set, so check if page id match anywhere. If it IS NOT found it it should not get the data, otherwise YES
wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '%,$id,%'
OR
# If exclude and include aren't set it should get the data
wp_postmeta.meta_key <> 'exkludera' AND wp_postmeta.meta_key <> 'include'
)
# END - Include exclude


";



$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{

// collect all fields in category 1 (eg. sidebar)
if($row['slug'] == 'category-1'){
$all_fields_from_category_1 = $all_fields_from_category_1.'
<div id="field-sidebar">
'.$row['post_content'].'
</div>
';}

// collect all fields in category 1 (eg. highlight)
if($row['slug'] == 'category-2'){
$all_fields_from_category_2 = $all_fields_from_category_2.'
<div id="field-highlight">
'.$row['post_content'].'
</div>
';}



} // end while



// sidebar
echo '<div id="container-sidebar">'.
$all_fields_from_category_1.
'</div>';


// highlight
echo '<div id="container-highlight">'.
$all_fields_from_category_1.
'</div>';

?>

新版本,新问题:

SELECT DISTINCT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-own-fields'

AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3', 'field4', 'field5', 'field6')
AND
(
wp_postmeta.meta_key = 'exlude' AND wp_postmeta.meta_value <> '$id'
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '$id'
OR wp_postmeta.meta_key <> 'exlude' AND wp_postmeta.meta_value <> 'include' #LAST ROW
)

即使设置了排除或包含,“#LAST ROW”也会返回数据。那是因为它匹配表 wp_postmeta 中的另外两行。

wp_postmeta 表示例:

meta_id     post_id     meta_key    meta_value
1           30          include     18
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

如果无法为 post_id 找到 meta_key inklude 或 exclude,我想返回数据...

当我想返回数据时的 senario 示例:

meta_id     post_id     meta_key    meta_value
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

知道如何解决这个问题吗? 声明说:如果没有为当前 ID 找到具有排除或包含的行。返回数据。

更多例子:

post_id 是 30。

如果 senario 是:

meta_id     post_id     meta_key    meta_value
1           30          include     18
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

然后我想取回 wp_posts.post_content、wp_posts.ID 和 wp_terms.slug,只要 $id 为 18。

如果 senario 是:

meta_id     post_id     meta_key    meta_value
1           30          exclude     18
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

然后我想取回 wp_posts.post_content、wp_posts.ID 和 wp_terms.slug,只要 $id 不是 18。

如果 senario 是:

meta_id     post_id     meta_key    meta_value
2           30          _edit_lock  1322225789:1
3           30          _edit_last  1

然后我想取回 wp_posts.post_content、wp_posts.ID 和 wp_terms.slug。

最佳答案

您的连接已正确完成。当删除注释和多余的行时,您会发现查询并没有那么复杂。我所做的唯一真正的改变是将 wp_terms.slug 的 OR 列表更改为 IN 语句。这样做只是为了减少代码重复、清理外观,而不是为了改变功能。

SELECT p.post_content, p.ID, t.slug
FROM wp_posts AS p
INNER JOIN wp_postmeta AS pm ON p.ID = pm.post_id
INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id
WHERE p.post_type = 'my-own-post-type'
   AND p.post_status = 'publish'
   AND t.slug IN
      ('category-1', 'category-2', 'category-3', 'category-4', 'category-5')
   AND
   (
      ( pm.meta_key = 'exclude' AND pm.meta_value <> '$id' )
         OR ( pm.meta_key = 'include' AND pm.meta_value = '$id' )
         OR ( pm.meta_key <> 'exclude' AND pm.meta_value <> 'include' )
   )

关于mysql - 使用 JOIN 在 Wordpress 中进行高级 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8279839/

相关文章:

javascript - 使用 Node 显示 SQL 值

php - 在 CodeIgniter 中处理并发请求

java - 将 CSV 导入 Java 中的 SQLite

php - SQL查询如何匹配4个结果中的3个

css - <a class ="none"> 的真正含义是什么?

wordpress - WordPress-允许编辑者管理主题小部件

php - 存储大量内容的建议方法是什么?

php - 将特殊字符如•¤♫†³™保存到数据库PHP中同时在php中回显它们

mysql - 将数据从 SQL Server/PostgreSQL 移动到 MySQL

javascript - 当页面上存在透明 PNG 时,站点上的所有 Javascript 在 Firefox 4 中执行得非常慢