【问题标题】:Getting 'meta_value' when searching for matching 'post_id' and 'meta_key', 'meta_value' combo搜索匹配的“post_id”和“meta_key”、“meta_value”组合时获取“meta_value”
【发布时间】:2018-02-16 11:34:09
【问题描述】:

postmeta table example

最终结果匹配一个 'post_id' 数组,按参加者人数排序。我知道这是一个 WP 后端,但我必须在 SQL 中完成,没有 'get_posts' 或任何与 WordPress 相关的东西。我想做的事情令人困惑,所以我会尽量清楚。

我必须开始:

  • $check_post_ids - 我需要检查的原始 post_ids 数组
  • $start_date - 'meta_value' 每个事件的 '_StartDate' 需要匹配。

我需要做什么:

我需要检查这三个 post_id 以查看它们是否具有匹配的开始日期。如果他们这样做了,我需要得到一个从参加者人数最多到最少的 post_id 数组。


目前我正计划使用多个 SELECT 和 foreach() 语句来执行此操作,但我觉得必须有一种更简单的方法来执行此操作(即一个 SELECT 和 foreach() )。这就是我现在正在做的事情。我还没有完成它,因为我觉得必须有一种更简单的方法来做到这一点。非常感谢 SQL 新手和任何帮助!

    $check_post_ids = array('484', '627', '982', '2435');       
    $start_date = '1963-10-20 19:30:00';            

    // iterate through array of post_ids and check if they have the same _StartDate
    foreach($check_post_ids as $id){
        $start_date_check = "SELECT * FROM `wp_postmeta` WHERE `post_id` =" . $id . " AND `meta_key` LIKE '_StartDate' AND `meta_value` = '" . $start_date . "'";
        $start_date_check_result = mysqli_query($conn, $start_date_check);

        // assign all post_ids with a matching _StartTime to a new array
        if (mysqli_num_rows($start_date_check_result) > 0) {
            while($row = mysqli_fetch_assoc($start_date_check_result)) {
                $matching_post_ids[] = $row['post_id'];

                // iterate through matching_post_ids array, get the _NumAttendees for each, and assign their id and _NumAttendees to an assoc array to be sorted
                foreach($matching_post_ids as $id){
                    $attendees_check = "SELECT meta_value FROM wp_postmeta WHERE post_id = " . $id . " AND meta_key = '_ecp_custom_2'";
                    $attendees_check_result = mysqli_query($conn, $attendees_check);
                    if($upvotes_check > 0){
                        while($row = mysqli_fetch_assoc($attendees_check_result)){
                            $sort_by_attendees['id'] = $id;
                            $sort_by_attendees['attendees'] = $row['meta_value'];
                            $sort_by_attendees_array[] = $sort_by_attendees;
                        }
                    }
                } 

【问题讨论】:

  • 再次搞砸了,我可以看到我的代码有点不正常,但希望它可以帮助你了解基本的想法。

标签: php mysql sql wordpress mysqli


【解决方案1】:

对于查询的第一部分,我认为您可以使用SQL IN 关键字来简化代码。基本上,它将您的第一个数组的角色替换为您所有的帖子 ID。然后,您可以编写这样的 SQL 查询:

SELECT meta_value 
FROM wp_postmeta 
WHERE meta_key = '_ecp_custom_2' 
AND post_id IN (SELECT post_id
FROM wp_postmeta 
WHERE post_id IN ('484', '627', '982', '2435')
AND meta_key LIKE '_StartDate' 
AND meta_value = '" . $start_date . "'")
ORDER BY meta_value DESC

有 2 个查询。括号中的第一个,子选择表 wp_postmeta 上的所有帖子 ID,其中 post_ids 在您的列表中有 ID,以及它们是否与您的开始日期匹配。然后,主查询根据您的第一个子查询(所有帖子 ID 以及您的开始日期)选择所有元值(我想是与会者)。

希望对你有帮助。

【讨论】:

  • 你这个炸弹!谢谢!以防万一有人试图复制和粘贴它,您必须将最后一个双引号 (") 从开始日期之后移动到 DESC 之后。我还需要 post_ids 是动态的,因为它们来自数组所以我在数组上使用了 implode(),所以我可以使用变量。再次感谢您的帮助,非常感谢!
猜你喜欢
  • 2019-11-06
  • 2016-01-22
  • 1970-01-01
  • 2023-04-04
  • 2016-03-22
  • 2017-03-31
  • 2020-04-21
  • 2015-03-03
  • 2013-04-08
相关资源
最近更新 更多