简体   繁体   English

有没有更好的方法来在mysql和PHP中查询此

[英]Is there a better way to query this in mysql and PHP

I am writing some custom code for wordpress using the postmeta table. 我正在使用postmeta表为wordpress编写一些自定义代码。 The items are stored in here and the common pk1 is the post_id. 这些项目存储在此处,并且公共pk1是post_id。

However what I am doing will have a bunch of clickable parts of the postmeta such as 但是,我正在做的事将有一堆可点击的部分,例如

  • source_site source_site
  • source_author source_author
  • source_rating source_rating

Or something similar. 或类似的东西。 Now the table will hold multiple post data for different post that have the same source_site or source_author. 现在,该表将为具有相同source_site或source_author的不同帖子保存多个帖子数据。

A user can then click on the source_author and they will be taken to a new table that contains all of the posts or articles that link back to this source_author. 然后,用户可以单击source_author,它们将被带到一个新表中,该表包含链接到该source_author的所有帖子或文章。

The issue is that the I am linking to a postmeta value rather than via the pk1 of the post-id. 问题是我链接到后元数值,而不是通过后id的pk1链接。

This means to get the rest of the data I am going to either have to do an additional query or do a join into the same table. 这意味着要获取其余数据,我将不得不执行其他查询或对同一张表进行联接。

This brings me to my question. 这使我想到了我的问题。 Would it be better to do the join or the subquery. 进行联接或子查询会更好。

The further problem is that the postmeta table stores all the various data and keys and values, so it makes it hard to query the other parts I want. 另一个问题是postmeta表存储所有各种数据以及键和值,因此很难查询我想要的其他部分。

so the table would look something like 所以桌子看起来像

   |meta_id|post_id|meta_key|meta_value|
   |1      | 2     |source_author | Dan Holmes |
   |2      | 2     |source_site   |http://somesite.com|

Just looking for the cleanest way to grab this data, all I can think otherwise is to grab the data first for the author with: 我只是想寻找最干净的方法来获取这些数据,否则我只能想到的是,首先使用以下方法为作者获取数据:

$the_post_ids = select post_id from postmeta where meta_key='source_author' and meta_value='Dan Holmes'

foreach($the_post_ids as $post_id) {
   select * from postmeta where post_id=$post_id;
}

now that is all very much pseudo code and i guess the problem is that once the data starts to get fairly large, I am concerned that this will end up with a lot of overheads for each section that can be clicked on and the fact i have to do multiple selects into a single array. 现在这都是伪代码,我想问题是一旦数据开始变得相当大,我担心这将导致每个部分都可以点击的大量开销,并且事实上在一个数组中进行多个选择。

Is there a better way to handle this? 有没有更好的方法来解决这个问题?

As you mention, performance becomes a concern as the database grows, so you'll want to limit the round-trips to the database to as few as possible. 正如您所提到的,随着数据库的增长,性能成为一个问题,因此,您希望将数据库的往返次数限制为尽可能少。 I'd pull back all the meta-table rows for the given author like this query: 我会像以下查询一样拉回给定作者的所有元表行:

SELECT * FROM postmeta WHERE post_id IN
    (SELECT post_id FROM postmeta
        WHERE meta_key='source_author' AND meta_value='Dan Holmes'
    )

Then write a foreach loop in PHP similar to the one you have, to handle the results. 然后在PHP中编写类似于您所拥有的foreach循环来处理结果。

EDIT 编辑

Here's a different option, without using the IN clause 这是一个不同的选项,不使用IN子句

SELECT meta1.* FROM postmeta meta1
    INNER JOIN 
        (SELECT DISTINCT postid FROM postmeta
             WHERE postmeta.meta_key = 'source_author' AND postmeta.meta_value = 'Dan Holmes'
        ) meta2 
    ON meta1.post_id = meta2.post_id

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

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