如何对Wordpress数据库进行comlex查询

时间:2018-04-12 05:57:16

标签: php sql wordpress

我试图对WordPress数据库进行复杂的查询,但它不起作用,所以我请求帮助。 首先,我需要从一个表中获取元值,然后使用它从另一个表中获取ID。 为此,我使用以下代码:

library(dplyr)
df %>% group_by(region = cumsum(c(1,diff(position))!=1)+1) %>%
  summarise_all(c("min", "max","mean")) %>%
  select(region, start = position_min, end = position_max, 
         min_case = case_min,  max_case = case_max, 
         mean_case = case_mean,  min_control = control_min) %>%
  as.data.frame()

#   region start end min_case max_case mean_case min_control
# 1      1     1   3        1        4       2.3           1
# 2      2     6   6       12       12      12.0           3
# 3      3     8  11        1       22      10.0           2

我想优化查询。试图在这里提出这样的请求,但它返回给我1而不是必要的值:

global $wpdb;
$event_ID   = 306;
$meta_value = $wpdb->get_var( "SELECT meta_value FROM wp_postmeta
    WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}'" );
$event_type = $wpdb->get_var( "SELECT post_title FROM wp_posts
    WHERE ID = '{$meta_value}'" );

请告诉我如何在我的案例中进行正确的复杂查询。

1 个答案:

答案 0 :(得分:0)

您应该能够将两个查询链接在一起:

SELECT post_title
FROM wp_posts
WHERE ID = (SELECT meta_value FROM wp_postmeta
    WHERE meta_key = 'driver_type' AND post_id = '{$event_ID}');

或者,您可以尝试进行加入:

SELECT p.post_title
FROM wp_posts p
INNER JOIN wp_postmeta m
    ON p.ID = m.meta_value
WHERE
    m.meta_key = 'driver_type' AND post_id = '{$event_ID}';