[英]I need help joining two queries to get one table as result
Hello I need help joining two queries I'm using to get the result in one query instead of two different 您好,我需要帮助加入两个我正在使用的查询,而不是两个不同的查询
The first query is: 第一个查询是:
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
The second one is: 第二个是:
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
Now both tables give me this result 现在两个表都给我这个结果
First 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
Second query: 第二个查询:
product_id url
83 www.img.com/puma.jpg
86 www.img.com/nike.jpg
What I want is to combine the queries to get something like: 我想要的是结合查询以得到类似的东西:
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
All help is very much appreciated, thanks. 非常感谢所有帮助。
( 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)
This will give you the required result. 这将为您提供所需的结果。
From your question it is like you just want combination of this two irrespective of their joins and all. 从您的问题来看,就像您只是想将这两个组合结合在一起,而不考虑它们的联接和所有联接。
You could join the two query 您可以加入两个查询
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
Query structure will be- 查询结构将是-
SELECT
A.*,B.Url
FROM
(
--Your First Query
)A
INNER JOIN
(
--Your Second Query
)B
ON A.Product_id = B.Product_id
Note: Use LEFT JOIN if possibilities are there to have less records in Query 2 compare to Query 1. 注意:如果查询2中的记录少于查询1,则使用LEFT JOIN。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.