【问题标题】:Joining two tables twice with different rows selected each time将两个表连接两次,每次选择不同的行
【发布时间】:2018-06-26 01:31:05
【问题描述】:

我必须将 wp_posts 与 wp_postsmeta 加入两次才能获得行。首先是post_type=product,然后是post_type=attachment

wp_posts:

╔════╦════════════╦═════════════════════════════════════════════════════╗
║ ID ║ post_type  ║ guid                                                ║
╠════╬════════════╬═════════════════════════════════════════════════════╣
║ 48 ║ product    ║ http://example.com/xyz                              ║
╚════╩════════════╩═════════════════════════════════════════════════════╝

wp_postsmeta

╔═════════╦═════════╦═══════════════╦═══════════╗
║ meta_id ║ post_id ║ meta_key      ║ meta_value║
╠═════════╬═════════╬═══════════════╬═══════════╣
║ 200     ║ 48      ║ _price        ║ 100       ║
╚═════════╩═════════╩═══════════════╩═══════════╝

查询:

接下来,我还想再次加入wp_postswp_postsmetapost_type = attachmentmeta_key =_thumbnail

╔════╦════════════╦═════════════════════════════════════════════════════╗
║ ID ║ post_type  ║ guid                                                ║
╠════╬════════════╬═════════════════════════════════════════════════════╣
║ 150║ attachment ║ http://example.com/xyz.png                          ║
╚════╩════════════╩═════════════════════════════════════════════════════╝

wp_postsmeta:

╔═════════╦═════════╦═══════════════╦═══════════╗
║ meta_id ║ post_id ║ meta_key      ║ meta_value║
╠═════════╬═════════╬═══════════════╬═══════════╣
║ 200     ║ 48      ║ _thumbnail    ║ 150       ║
╚═════════╩═════════╩═══════════════╩═══════════╝

然后我使用结果的 meta_value 并再次将其与 wp_posts(主键 wp_posts.id = wp_postsmeta.meta_value)连接,这样我就可以从他们那里获得产品的特色图片。

以下是我的完整查询

SELECT p1.ID, p1.guid, p3.guid
FROM wp_posts p1 
     JOIN wp_postmeta p2
     ON p1.ID = p2.post_id AND
        p2.meta_key = '_price' AND
        p1.post_type = 'product' AND
        p1.post_status = 'publish' 
     JOIN wp_posts p3
     ON p3.ID = p2.post_id AND 
        p2.meta_key = '_thumbnail_id' 
     JOIN wp_postmeta p4
     ON p4.post_id = p3.ID AND
        p3.post_type = 'attachment';

上述查询返回空结果(不应该为空,而是返回如下表)

╔════╦════════════╦═════════════════════════╦════════════════════════════╗
║ ID ║ post_type  ║ guid                    ║  guid                      ║
╠════╬════════════╬═════════════════════════╬════════════════════════════╣
║ 48 ║ product    ║ http://example.com/xyz  ║ http://example.com/xyz.png ║
╚════╩════════════╩═════════════════════════╩════════════════════════════╝

【问题讨论】:

  • 您将在p2 上加入p3p2.meta_key = '_thumbnail_id',但您之前已经过滤了p2p2.meta_key = '_price',那么您将无法获得结果。
  • @Troopers:如何解决这个问题?
  • @FahadUddin 除了@Troopers 提到的内容之外,您还加入了p1.ID = p2.post_idp3.ID = p2.post_id。这意味着来自 p1 和 p3 的记录需要具有相同的 ID,但是在您提供的示例中,第一条记录的 ID = 48,第二条记录的 ID = 150。这两条记录之间没有联系。

标签: mysql wordpress join


【解决方案1】:

试试这个查询

SELECT posts.post_title,posts.ID, posts.post_name, (SELECT guid from wp_posts AS thumbnailpost INNER JOIN `wp_postmeta` ON (`wp_postmeta`.meta_value = thumbnailpost.ID) where `wp_postmeta`.meta_key='_thumbnail_id' AND `wp_postmeta`.post_id = 2856) AS thumbnail FROM `wp_posts` AS posts where posts.post_type = 'product' and posts.post_status='publish' group by posts.ID limit 100

更新

你想要这样吗?

SELECT p1.ID, p1.guid,
(select p.guid from wp_posts as p where p2.meta_value=p.ID and post_type='attachment') img
FROM wp_posts p1 
     JOIN wp_postmeta p2
     ON p1.ID = p2.post_id AND       
        p1.post_type = 'product' AND
        p1.post_status = 'publish' AND
         p2.meta_key = '_thumbnail_id' 
     JOIN wp_posts p3
     ON p3.ID = p2.post_id

【讨论】:

  • 这会得到其他结果,但最后一列为空
  • 你必须使用子查询
猜你喜欢
  • 2011-04-02
  • 1970-01-01
  • 2017-02-20
  • 2014-05-04
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多