合并2个SQL查询。

时间:2018-07-30 09:31:42

标签: mysql sql wordpress

我为Wordpress的插件编码(或应该重新编码)。

我使用meta.key:'_rl_distance_value':

来查询年份,月份和总距离pr月份。

SELECT DATE_FORMAT( wp_posts.post_date, '%Y' ) AS Runyear, MONTH( wp_posts.post_date ) AS Runmonth, SUM( wp_postmeta.meta_value ) AS Distance
FROM wp_postmeta
INNER JOIN wp_posts ON ( wp_postmeta.post_id = wp_posts.id )
WHERE wp_postmeta.meta_key = '_rl_distance_value'
AND wp_posts.post_status = 'publish'
AND wp_postmeta.post_id = wp_posts.id
AND year(wp_posts.post_date)= '2010'
GROUP BY DATE_FORMAT( wp_posts.post_date, '%Y-%m' )

它打印出这样的内容:

Runyear     Runmonth    Distance    
2010    1   76260
2010    2   88670
2010    3   106490
2010    4   57199
2010    5   66460
2010    6   84480
2010    7   54490
2010    8   45090
2010    9   27140
2010    10  70600
2010    11  51000

我也有此查询,可使用meta.key:“ _ rl_time_value”获取每个月的总运行时间

SELECT DATE_FORMAT( wp_posts.post_date, '%Y-%m' ) AS Runyear, MONTH( wp_posts.post_date ) AS Runmonth, ((SUM( time_to_sec( STR_TO_DATE( wp_postmeta.meta_value, '%T' ) ) ) )/3600) AS Runtimetotal
FROM wp_postmeta
INNER JOIN wp_posts ON ( wp_postmeta.post_id = wp_posts.id )
WHERE wp_postmeta.meta_key = '_rl_time_value'
AND wp_posts.post_status = 'publish'
AND year(wp_posts.post_date)= '2010'
GROUP BY DATE_FORMAT( wp_posts.post_date, '%Y-%m' )

它打印类似:

Runyear     Runmonth    Runtimetotal    
2010-01     1   7.8928
2010-02     2   9.1667
2010-03     3   10.7083
2010-04     4   5.7497
2010-05     5   6.9561
2010-06     6   8.5594
2010-07     7   5.5767
2010-08     8   4.6139
2010-09     9   2.7233
2010-10     10  7.3953
2010-11     11  4.5858

如何合并这两个查询以具有这样的结构

Runyear     Runmonth    Distance    Runtimetotal 

我不知道该怎么办?

你能帮忙吗?

3 个答案:

答案 0 :(得分:0)

在下面使用左右联接和联合sudo查询

all_url

答案 1 :(得分:0)

尝试一下:

SELECT DATE_FORMAT( wp_posts.post_date, '%Y-%m' ) AS Runyear, MONTH( wp_posts.post_date ) AS Runmonth, 
((SUM( case when (wp_postmeta.meta_key = '_rl_time_value' then time_to_sec( STR_TO_DATE( wp_postmeta.meta_value, '%T' ) ) ) else 0 end )/3600) AS Runtimetotal, SUM( case when p_postmeta.meta_key = '_rl_distance_value' then wp_postmeta.meta_value else 0) AS Distance
FROM wp_postmeta
INNER JOIN wp_posts ON ( wp_postmeta.post_id = wp_posts.id )
WHERE (wp_postmeta.meta_key = '_rl_time_value' ||wp_postmeta.meta_key = '_rl_distance_value')
AND wp_posts.post_status = 'publish'

AND year(wp_posts.post_date)= '2010'
GROUP BY DATE_FORMAT( wp_posts.post_date, '%Y-%m' )

说明:如果运行时的键为time_value,则添加时间,而距离为0。这意味着结果将不会更改。距离值也一样。

或者,您可以:

select A.*, B.Runtimetotal 
from A 
inner join B on A.Runmonth=B.Runmonth and A.Runyear=B.RunYear;

其中A和B是查询的结果

答案 2 :(得分:0)

我认为您可以只使用条件聚合:

SELECT DATE_FORMAT( p.post_date, '%Y-%m' ) AS Runyear,
       MONTH( p.post_date ) AS Runmonth,
       SUM(CASE WHEN pm.meta_key = '_rl_distance_value' THEN wp_postmeta.meta_value END) AS Distance,          
       SUM(time_to_sec(STR_TO_DATE(CASE WHEN pm.meta_key = '_rl_time_value' THEN pm.meta_value END, '%T')))/3600 AS Runtimetotal
FROM wp_postmeta pmm INNER JOIN
     wp_posts p
     ON pm.post_id = p.id
WHERE pm.meta_key IN ('_rl_distance_value', '_rl_time_value') AND
      p.post_status = 'publish' AND
      year(p.post_date)= 2010
GROUP BY DATE_FORMAT(p.post_date, '%Y-%m' )