combine two select statements (append columns) - mysql

I have 2 Wordpress database queries, the first shows all posts + categories. The second shows all posts + custom fields.
What I'm looking for is listing all posts + categories + custom fields
Query 1: list posts + categories
SELECT ID,
post_title,
(SELECT group_concat(wp_terms.name separator ", ") FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id =
wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy="category" and wp_posts.ID = wpr.object_id ) AS
"Categories"
FROM wp_posts WHERE post_type = "post" AND post_status = "publish"
+------+--------------------------------------+------------+
| ID | post_title | Categories |
+------+--------------------------------------+------------+
Query 2: list posts + custom fields
SELECT ID, post_title, pm1.meta_value as "Amazon.com", pm2.meta_value
as "Amazon.co.uk" FROM wp_posts
LEFT JOIN wp_postmeta pm1 ON ID = pm1.post_id AND
pm1.meta_key = "Amazon.com"
LEFT JOIN wp_postmeta pm2 ON ID = pm2.post_id AND
pm2.meta_key = "Amazon.co.uk"
WHERE post_type = "post" AND post_status = "publish"
+------+-----------------------+--------------+--------------+
| ID | post_title | Amazon.com | Amazon.co.uk |
+------+-----------------------+--------------+--------------+
How can I combine the results so that I can list all posts + categories + custom fields?
+------+-------------+-------------+--------------+--------------+
| ID | post_title | Categories | Amazon.com | Amazon.co.uk |
+------+-------------+-------------+--------------+--------------+

Try using the following query:
SELECT ID, post_title, pm1.meta_value as "Amazon.com", pm2.meta_value as "Amazon.co.uk" ,
(SELECT group_concat(wp_terms.name separator ", ") FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id =
wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy="category" and wp_posts.ID = wpr.object_id ) AS "Categories"
FROM wp_posts
LEFT JOIN wp_postmeta pm1 ON ID = pm1.post_id AND
pm1.meta_key = "Amazon.com"
LEFT JOIN wp_postmeta pm2 ON ID = pm2.post_id AND
pm2.meta_key = "Amazon.co.uk"
WHERE post_type = "post" AND post_status = "publish"

In order to get all the columns in the same result is to 'merge' the two queries together and SELECT the unique columns listed in each of the queries.
Something like this should do the trick:
SELECT
ID,
post_title,
(SELECT
GROUP_CONCAT(wp_terms.name SEPARATOR ", ")
FROM
wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id=wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id= wp_term_taxonomy.term_taxonomy_id
WHERE
taxonomy="category"
AND wp_posts.ID=wpr.object_id
) AS `Categories`,
GROUP_CONCAT(pm1.meta_value) AS `Amazon.com`,
GROUP_CONCAT(pm2.meta_value) AS `Amazon.co.uk`
FROM
wp_posts
LEFT JOIN wp_postmeta AS `pm1` ON pm1.post_id=ID AND pm1.meta_key="Amazon.com"
LEFT JOIN wp_postmeta AS `pm2` ON pm2.post_id=ID AND pm2.meta_key="Amazon.co.uk"
WHERE
post_type="post"
AND post_status="publish"
GROUP BY
ID;
Let me know if there are any problems.
Hope this helps,

Related

Woocommerce Mysql query to fetch products and varables

Trying to fetch products ID and the products variable ID in Mysql.
My category is 7006.
This query gives me only the general product, and not the product variables values.
SELECT post.ID, post.post_title, metavalue1.meta_value AS MetaValue1, metavalue2.meta_value AS MetaValue2
FROM wp_posts post
LEFT JOIN wp_postmeta metavalue1 ON post.ID = metavalue1.post_id
AND '_enable_colorlab' = metavalue1.meta_key
LEFT JOIN wp_postmeta metavalue2 ON post.ID = metavalue2.post_id
AND '_wcpa_product_meta' = metavalue2.meta_key
LEFT JOIN wp_term_relationships rs ON rs.object_id = post.ID
WHERE rs.term_taxonomy_id ='7006';
This query gives me all variables ID i need
SELECT post.ID, post.post_title FROM wp_posts post
INNER JOIN wp_postmeta pa ON pa.post_id = post.ID
INNER JOIN wp_term_relationships rs ON rs.object_id = post.ID
WHERE rs.term_taxonomy_id ='7006';
How can i get first query to include all product and variables values and not the general product value?
IT will return variation products with variant id.
SELECT wp.id AS `Product Id`, wpv.id AS `Variant Id`, wp.post_title, wpv.post_title, wpv.post_excerpt
FROM wp_posts wp
INNER JOIN wp_term_relationships r ON wp.ID = r.object_id
INNER JOIN wp_term_taxonomy tt ON r.term_taxonomy_id = 7006
INNER JOIN wp_terms t ON t.term_id = tt.term_id
INNER JOIN wp_posts wpv ON wp.id = wpv.post_parent
LEFT JOIN wp_postmeta wpm ON wp.ID = wpm.post_id
WHERE tt.taxonomy = 'product_type'
AND t.name = 'variable'
AND wpv.post_type != 'attachment'
AND wpm.meta_key = '_enable_colorlab'
You can try something like this:
select
p.id,
p.post_title,
group_concat(concat(m.meta_key, ':', m.meta_value))
from
wp_posts as p
left join wp_postmeta as m on m.post_id = p.id
left join wp_term_relationships rs on rs.object_id = p.id
where
rs.term_taxonomy_id = '7006'
group by
p.id,
p.post_title
In PHP you can explode concatenated string of values

Get list of products that don't have a specific taxonomy in WooCommerce

I've hit an issue trying to find out what products are missing suppliers on a website with tens of thousands of products.
I need an MySQL query to determine which products are missing the taxonomy "suppliers" through phpmyadmin.
So far I've pieced this together from various answers:
SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON (wm.`post_id` = wp.`ID`)
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON (wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`)
INNER JOIN wp_terms wt ON (wt.`term_id` = wtt.`term_id`)
AND wtt.`taxonomy` = 'suppliers'
WHERE post_type = 'product'
GROUP BY wp.ID
I've tried numerous things and cannot get it to work.
The problem is with you query is that you are not excluding product that has suppliers that that product has some other attributes, so you can use NOT EXIST or NOT IN, I have written the query using NOT EXIST.
SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wm.`post_id` = wp.`ID`
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE post_type = 'product'
AND NOT EXISTS
(SELECT `object_id`
FROM `wp_term_relationships` AS wtr_inner
WHERE `term_taxonomy_id` IN
(SELECT term_taxonomy_id
FROM `wp_term_taxonomy`
WHERE `taxonomy` = 'suppliers')
AND wtr.object_id = wtr_inner.object_id )
GROUP BY wp.ID
Another version which will be a bit faster if you know all the suppliers term_taxonomy_id So you can modify the above query as
SELECT *
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wm.`post_id` = wp.`ID`
INNER JOIN wp_term_relationships wtr ON (wp.`ID` = wtr.`object_id`)
INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE post_type = 'product'
AND NOT EXISTS
(SELECT `object_id`
FROM `wp_term_relationships` AS wtr_inner
WHERE `term_taxonomy_id` IN (27,28,29) -- replace it will all suppliers term_taxonomy_id
AND wtr.object_id = wtr_inner.object_id )
GROUP BY wp.ID
Hope this helps!

Show the values in one row with same id in sql

I have Three tables and I join the table and retrieve all the values in this query
sql query:
SELECT wp_term_relationships.object_id, wp_posts.post_title,
wp_postmeta.meta_key, wp_postmeta.meta_value
FROM `wp_posts` , wp_postmeta, wp_term_relationships
WHERE (wp_posts.ID = wp_postmeta.post_id)
AND (wp_postmeta.post_id = wp_term_relationships.object_id)
AND wp_term_relationships.term_taxonomy_id =33
Its Show Table structure is this
object_id post_title meta_key meta_value
302 CHICKEN CHOW MEIN post_image url1
302 CHICKEN CHOW MEIN price 6.95
I want Show the OBject Id in one and merge the two rows in one column i want like this
object_id post_title meta_value(price) meta_value(post_image)
302 CHICKEN CHOW MEIN 6.95 url1
You could join to wp_postmeta twice, using more usual join syntax
SELECT rel.object_id, p.post_title,
meta_price.meta_key, meta_price.meta_value,
meta_image.meta_key, meta_image.meta_value
FROM `wp_posts` p
inner join wp_postmeta meta_price on meta_price.post_id = p.ID
inner join wp_postmeta meta_image on meta_image.post_id = p.ID
inner join wp_term_relationships rel on rel.objet_id = p.ID
WHERE rel.term_taxonomy_id = 33
and meta_price.meta_key = 'price'
and meta_image.meta_key = 'post_image'
(Syntax not checked – no access to a WordPress db here – and you'll need to change the selected columns slightly.)
If it was me, I'd simply stick with your existing query, and handle the pivot in the application code, so.
*PHP goes here*
$query = "
SELECT r.object_id
, p.post_title
, m.meta_key
, m.meta_value
FROM wp_posts p
JOIN wp_postmeta m
ON m.post_id = p.ID
JOIN wp_term_relationships r
ON r.object_id = m.post_id
WHERE r.term_taxonomy_id = 33
ORDER
BY post_title
, meta_key;
";
*More PHP goes here*
Syntax not checked, but something like this?
SELECT object_id, post_title, max(meta_key_price) as key_price, max(meta_key_image) as key_image FROM
(
SELECT
wp_term_relationships.object_id,
wp_posts.post_title,
case meta_key when 'price' then meta_value else null end as meta_key_price,
case meta_key when 'post_image' then meta_value else null end as meta_key_image
FROM `wp_posts` , wp_postmeta, wp_term_relationships
WHERE (wp_posts.ID = wp_postmeta.post_id)
AND (wp_postmeta.post_id = wp_term_relationships.object_id)
AND wp_term_relationships.term_taxonomy_id =33
) as t group by object_id

How to merge two SQL queries to get latest Wordpress Post and Featured Image

I'm trying to show latest Post Excerpt, Post Title and Featured Image on an ASP page. To simplify the query I add the Permalink as a custom field for each Post. I have a query that gets all except Featured Image and I have another query that gets Featured image but I can't work out how to merge them into one query.
// Gets Post Excerpt and Post Title
SELECT
wp_posts.id,
wp_posts.post_title,
wp_postmeta.meta_value,
wp_postmeta.meta_key,
wp_posts.post_excerpt
FROM
wp_postmeta
INNER JOIN wp_posts p ON (wp_postmeta.post_id = wp_posts.ID)
WHERE post_id IN (
SELECT wp_posts.id
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
AND meta_key = 'my_permalink'
ORDER BY post_date, wp_posts.id
)
ORDER BY wp_posts.post_date DESC, wp_postmeta.post_id
LIMIT 2
// Gets Featured Images for a Post
SELECT p.*
FROM wp_postmeta AS pm
INNER JOIN wp_posts AS p ON pm.meta_value=p.ID
WHERE pm.post_id = $ID
AND pm.meta_key = '_thumbnail_id'
ORDER BY p.post_date DESC
Can anyone help me merge these queries? Thanks.
Sample data to be returned:
ID | post_title | post_excerpt | meta_value_my_permalink | featured_image_guid
** UPDATE *
I've managed to get the following which works fine except I can't get more that one row as I get an error when I try and use 'IN' in a subquery
e.g. pm2.post_id IN (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 2)
SELECT
p.post_title,
p.post_excerpt,
pm.meta_value AS permalink,
p2.guid as thumbnail,
p2.post_title as image_alt
FROM
wp_postmeta pm
INNER JOIN wp_posts p ON (pm.post_id = p.ID),
wp_postmeta pm2
INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
pm.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND
pm.meta_key = 'my_permalink' AND
pm2.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND
pm2.meta_key = '_thumbnail_id'
It is difficult to see what you are trying to get without some sample data but I think that you know the post ID and need the info from both the my_permakink and _thumbnail_id keys?
Since I'm on a tablet I will give you the bare bones only and let you limit the result set and filter it.
Select *
From wp_posts p
Inner join
Wp_postmeta pm1 on p.id = pm1.post_id and metakey = 'my_permalink'
Inner join
Wp_postmeta pm2 on p.id = pm1.post_id and metakey = '_thumbnail_id'
This will give you every post that has an image with the info for both metakeys. If you want the info for posts without an image change the second inner join to a left join.
A bunch of joins and a view as the subquery has given me what I need.
SELECT
p.post_title,
p.post_excerpt,
pm.meta_value AS permalink,
p2.guid AS thumbnail,
p2.post_title AS thumbnail_alt
FROM
wp_postmeta pm
INNER JOIN wp_posts p ON (pm.post_id = p.ID)
INNER JOIN wp_postmeta pm2 ON (pm2.post_id = p.ID)
INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
pm.meta_key = 'my_permalink' AND
pm2.meta_key = '_thumbnail_id' AND
p.ID IN (SELECT * FROM vwLatestPostIds)
Use this to retrieve the post title, post id, post featured thumbnail url, post category etc -
"SELECT a.ID id, a.post_title title, a.post_content content,taxonomy, name, a.post_date_gmt postdate,max(c.guid) img_url
FROM kb_posts a
JOIN kb_term_relationships tr
ON (a.id = tr.object_id)
JOIN kb_term_taxonomy tt
ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy='category')
JOIN kb_terms t
ON (t.term_id = tt.term_id AND t.term_id = $user_id)
LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date from kb_posts
where post_type='attachment' GROUP BY post_parent) b
on a.id=b.post_parent
LEFT JOIN kb_posts c
on c.post_parent=a.id
and c.post_type='attachment'
and b.latest_image_date = c.post_date_gmt where c.guid IS NOT NULL
GROUP BY a.ID ORDER BY a.ID DESC LIMIT 5"

SQL query to extract all WordPress posts with categories

I need to extract all posts from my WordPress DB along with the associated categories and not sure how to write this query. I've taken a couple of stabs at it already with no joy and would appreciate the help?
EDIT: Here's what I have tried already:
SELECT post_title, wpr.object_id, wp_terms.name
FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts ON ID = wpr.object_id
WHERE taxonomy = 'category'
AND post_type = 'post'
ORDER by post_title
This seems to work but it returns 1,553 where I know I only have 1343 in my DB.
EDIT:
We did the same thing on another SQL query a little while ago and found that it was pulling in the revisions and other post types but thought that this was resolved using post_type = 'post'
EDIT:
Upon looking at the number of categories in the DB, I come up with a total number of 216, 6 off the number if you subtract 1553 - 1343 = 216. So I think this total number of 1553 is coming from the wp_terms table which needs to be excluded and only those that are active with published posts should be shown?
EDIT:
The other possibility is that each post can have multiple categories, hence the reason for having more posts (1553). So how could I separate each posts into multiple categories?
Many thanks!
This is the final answer that worked for me.
SELECT DISTINCT
post_title
, post_content
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (US\$)' AND wp_postmeta.post_id = wp_posts.ID) AS "Asking Price (US\$)"
,(SELECT group_concat(wp_terms.name separator ', ')
FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy= 'category' and wp_posts.ID = wpr.object_id
) AS "Categories"
,(SELECT group_concat(wp_terms.name separator ', ')
FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy= 'post_tag' and wp_posts.ID = wpr.object_id
) AS "Tags"
FROM wp_posts
WHERE post_type = 'post'
ORDER BY
post_title
, post_content
/* Query for fetch post/posts using post user, post category and post_title */
$query ="SELECT wp_posts.post_title, wp_posts.post_content, wp_posts.comment_count, wp_users.display_name, wp_terms.name
FROM wp_posts
JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
JOIN wp_users ON (wp_posts.post_author = wp_users.ID)
WHERE wp_term_taxonomy.term_id IN ($bycat)
AND wp_users.ID = $byuser
AND wp_posts.post_type = 'post'
AND (wp_posts.post_content LIKE '$bytitle' OR wp_posts.post_title LIKE '$bytitle')
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_modified DESC";
/*---- FOR DISPLAY RESULT -----*/
$resultfirst = $wpdb->get_results($query);
foreach( $resultfirst as $result ){
echo $result->post_title .'';
echo $result->display_name.'';
echo $result->name.'';
echo $result->comment_count.'';
}