0

我正在尝试在 ASP 页面上显示最新的帖子摘录、帖子标题和特色图片。为了简化查询,我将永久链接添加为每个帖子的自定义字段。我有一个查询可以获取除特色图片之外的所有内容,我还有另一个查询可以获取特色图片,但我不知道如何将它们合并到一个查询中。

// 获取文章摘录和文章标题

SELECT
 wp_posts.id, 
 wp_posts.post_title, 
 wp_postmeta.meta_value, 
 wp_postmeta.meta_key, 
 wp_posts.post_excerpt 
 FROM 
 wp_postmeta 
 INNER JOIN wp_posts p ON (wp_postmeta.post_id = wp_posts.ID) 
 WHERE post_id IN (
 SELECT wp_posts.id 
 FROM wp_posts 
 WHERE post_type = 'post' 
 AND post_status = 'publish' 
 AND meta_key = 'my_permalink' 
 ORDER BY post_date, wp_posts.id
 ) 
 ORDER BY wp_posts.post_date DESC, wp_postmeta.post_id 
 LIMIT 2

// 获取帖子的特色图片

 SELECT p.*
      FROM wp_postmeta AS pm
     INNER JOIN wp_posts AS p ON pm.meta_value=p.ID 
     WHERE pm.post_id = $ID
       AND pm.meta_key = '_thumbnail_id' 
     ORDER BY p.post_date DESC 

谁能帮我合并这些查询?谢谢。

要返回的样本数据:ID | post_title | post_excerpt | meta_value_my_permalink | 特色图片指南

** 更新* 我已经设法得到以下工作正常,除了我不能得到更多的一行,因为当我尝试在子查询中使用“IN”时出现错误,例如 pm2.post_id IN (SELECT wp_posts. id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 2)

SELECT 
  p.post_title,
  p.post_excerpt,
  pm.meta_value AS permalink,
  p2.guid as thumbnail,
  p2.post_title as image_alt
FROM
  wp_postmeta pm
  INNER JOIN wp_posts p ON (pm.post_id = p.ID),
  wp_postmeta pm2
  INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
  pm.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND 
  pm.meta_key = 'my_permalink' AND 
  pm2.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND 
  pm2.meta_key = '_thumbnail_id'
4

3 回答 3

1

如果没有一些示例数据,很难看到你想要得到什么,但我认为你知道帖子 ID 并且需要来自 my_permakink 和 _thumbnail_id 键的信息?因为我在平板电脑上,所以我只会给你简单的骨头,让你限制结果集并过滤它。

Select *
From wp_posts p
     Inner join
     Wp_postmeta pm1 on p.id = pm1.post_id and metakey = 'my_permalink'
     Inner join
     Wp_postmeta pm2 on p.id = pm1.post_id and metakey = '_thumbnail_id'

这将为您提供每个包含两个元键信息的图像的帖子。如果您想要没有图像的帖子的信息,请将第二个内部连接更改为左连接。

于 2012-12-02T07:50:07.890 回答
0

使用它来检索帖子标题、帖子 ID、帖子特色缩略图 url、帖子类别等 - “选择 a.ID id、a.post_title 标题、a.post_content 内容、分类、名称、a.post_date_gmt postdate、max(c. guid) img_url FROM kb_posts a JOIN kb_term_relationships tr ON (a.id = tr.object_id) JOIN kb_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy='category') JOIN kb_terms t ON (t.term_id = tt .term_id 和 t.term_id = $user_id)

            LEFT JOIN
            (select post_parent, max(post_date_gmt) as latest_image_date from kb_posts
            where post_type='attachment' GROUP BY post_parent) b 
            on a.id=b.post_parent

            LEFT JOIN kb_posts c
            on c.post_parent=a.id 
            and c.post_type='attachment' 
            and b.latest_image_date = c.post_date_gmt where c.guid IS NOT NULL


            GROUP BY a.ID ORDER BY a.ID DESC LIMIT 5"
于 2014-03-01T20:34:36.070 回答
0

一堆连接和一个视图作为子查询给了我我需要的东西。

SELECT 
  p.post_title,
  p.post_excerpt,
  pm.meta_value AS permalink,
  p2.guid AS thumbnail,
  p2.post_title AS thumbnail_alt
FROM
  wp_postmeta pm
  INNER JOIN wp_posts p ON (pm.post_id = p.ID)
  INNER JOIN wp_postmeta pm2 ON (pm2.post_id = p.ID)
  INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
  pm.meta_key = 'my_permalink' AND
  pm2.meta_key = '_thumbnail_id' AND 
  p.ID IN (SELECT * FROM vwLatestPostIds)
于 2012-12-03T13:32:06.693 回答