你好,我需要帮助连接两个我正在使用的查询,以便在一个查询中而不是两个不同的查询中获取结果
第一个查询是:
SELECT p.ID as product_id, p.post_title,
max( CASE WHEN pm.meta_key = 'product_gender' and p.ID = pm.post_id THEN pm.meta_value END ) as gender,
max( CASE WHEN pm.meta_key = '_price' and p.ID = pm.post_id THEN pm.meta_value END ) as price,
max( CASE WHEN pm.meta_key = 'product_color' and p.ID = pm.post_id THEN pm.meta_value END ) as color,
max( CASE WHEN pm.meta_key = 'product_category' and p.ID = pm.post_id THEN pm.meta_value END ) as category,
max( CASE WHEN pm.meta_key = 'product_size' and p.ID = pm.post_id THEN pm.meta_value END ) as size
FROM
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
group by
p.ID
第二个是:
SELECT parentmeta.post_id as post_id,
concat((select option_value from wp_options where option_name ='siteurl' limit 1),'/wp-content/uploads/',childmeta.meta_value) as url
FROM wp_postmeta childmeta
INNER JOIN wp_postmeta parentmeta ON (childmeta.post_id=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id' and childmeta.meta_key = '_wp_attached_file'
AND parentmeta.post_id = p.ID //I want to used the same ID as the previous query
现在两个表都给我这个结果
第一个查询:
product_id post_title gender price color category size
83 Puma-Tshirt Man 19 RED Tshirt Medium
86 Nike-Pants Man 49 BLACK Pants Medium
第二个查询:
product_id url
83 www.img.com/puma.jpg
86 www.img.com/nike.jpg
我想要的是结合查询以得到类似的东西:
product_id post_title gender price color category size url
83 Puma-Tshirt Man 19 RED Tshirt Medium www.img.com/puma.jpg
86 Nike-Pants Man 49 BLACK Pants Medium www.img.com/nike.jpg
非常感谢所有帮助。
答案 0 :(得分:2)
( SELECT p.ID as product_id, p.post_title,
max( CASE WHEN pm.meta_key = 'product_gender' and p.ID = pm.post_id THEN pm.meta_value END ) as gender,
max( CASE WHEN pm.meta_key = '_price' and p.ID = pm.post_id THEN pm.meta_value END ) as price,
max( CASE WHEN pm.meta_key = 'product_color' and p.ID = pm.post_id THEN pm.meta_value END ) as color,
max( CASE WHEN pm.meta_key = 'product_category' and p.ID = pm.post_id THEN pm.meta_value END ) as category,
max( CASE WHEN pm.meta_key = 'product_size' and p.ID = pm.post_id THEN pm.meta_value END ) as size
FROM
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
group by
p.ID )
UNION
(SELECT parentmeta.post_id as post_id,
concat((select option_value from wp_options where option_name ='siteurl' limit 1),'/wp-content/uploads/',childmeta.meta_value) as url
FROM wp_postmeta childmeta
INNER JOIN wp_postmeta parentmeta ON (childmeta.post_id=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id' and childmeta.meta_key = '_wp_attached_file'
AND parentmeta.post_id = p.ID //I want to used the same ID as the previous query)
这将为您提供所需的结果。
从您的问题来看,就像您只是想将这两个组合结合在一起,而不论它们的联接和所有联接。
答案 1 :(得分:1)
您可以加入两个查询
select t1.*, t2.*
from (
SELECT p.ID as product_id, p.post_title,
max( CASE WHEN pm.meta_key = 'product_gender' and p.ID = pm.post_id THEN pm.meta_value END ) as gender,
max( CASE WHEN pm.meta_key = '_price' and p.ID = pm.post_id THEN pm.meta_value END ) as price,
max( CASE WHEN pm.meta_key = 'product_color' and p.ID = pm.post_id THEN pm.meta_value END ) as color,
max( CASE WHEN pm.meta_key = 'product_category' and p.ID = pm.post_id THEN pm.meta_value END ) as category,
max( CASE WHEN pm.meta_key = 'product_size' and p.ID = pm.post_id THEN pm.meta_value END ) as size
FROM wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
group by p.ID
) t1
inner join (
SELECT parentmeta.post_id as post_id,
concat((select option_value from wp_options where option_name ='siteurl' limit 1),'/wp-content/uploads/',childmeta.meta_value) as url
FROM wp_postmeta childmeta
INNER JOIN wp_postmeta parentmeta ON (childmeta.post_id=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id' and childmeta.meta_key = '_wp_attached_file'
) t2 on t1.product_id = t2.post_id
答案 2 :(得分:1)
查询结构将是-
SELECT
A.*,B.Url
FROM
(
--Your First Query
)A
INNER JOIN
(
--Your Second Query
)B
ON A.Product_id = B.Product_id
注意:如果查询2中的记录少于查询1,则使用LEFT JOIN。