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
Related
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,
I have a custom post type and a taxonomy, where I can put more the one value to the taxonomy, in this case the taxonomy is the city. I want to make a query that the result is the posts that only have ONE value set. Example: I want all posts that the taxnomy is only "term_id = 3" that is "sao-paulo-sp".
But I have some posts that has more than one value set to it, like "sao-paulo-sp, city 2, city 3 city 4, etc" and this kinda of post keep showing up in my results.
SELECT wp.ID, wp.post_title, wt.term_id FROM wp_posts wp
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 wtt.taxonomy = 'cities'
AND wt.slug = 'sao-paulo-sp'
ORDER BY wp.ID
I would like that the result was the posts that only have "sao-paulo-sp" or "term_id = 3' as his value to the taxonomy cities
If I understand correctly, you want something like this:
SELECT wp.ID, wp.post_title, wt.term_id
FROM wp_posts wp 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 wtt.taxonomy = 'cities'
GROUP BY wp.ID
HAVING COUNT(*) = 1 AND MAX(wt.slug) = 'sao-paulo-sp'
ORDER BY wp.ID;
I am not sure what wt.term_id is doing in the SELECT list.
Srry, my english was not good enough to specify my question, if help anyone I solved my problem doing a NOT IN in a select that has term_id different than tem_id 3.
SELECT wp.ID, wp.post_title, wt.slug
FROM wp_posts wp
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 wtt.taxonomy = 'cidades'
AND wp.ID NOT IN
(SELECT wp.ID
FROM wp_posts wp
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`
WHERE wtt.taxonomy ='cidades' and wtt.term_id <> 3)
ORDER BY wp.ID
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!
I'm querying a wordpress mysql db outside the php language and I don't know how to solve the following problem: each post has taxonomies with some value. There's a value called 'calificaciones' which I'd like to filter on. This is my current query:
SELECT wp_posts.post_title, IF (wp_term_taxonomy.taxonomy = 'calificaciones', wp_terms.slug, 'no') as calificacion
FROM wp_posts
JOIN wp_term_relationships ON (wp_term_relationships.object_id = wp_posts.ID)
JOIN wp_term_taxonomy ON (wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
AND wp_posts.post_type = 'ultimas-noticias'
#AND wp_term_taxonomy.taxonomy = 'calificaciones'
The problem with this query is that I'll get the post_title multiple times(because there are others taxonomies for each post). I'd like to get the post title and a flag yes-no, if the post has that taxonomy value or not.
Instead of joining the taxonomies on the same query you could use a sub query for the calificacion column.
SELECT wp_posts.post_title, CASE WHEN EXISTS((SELECT *
FROM wp_term_relationships
JOIN wp_term_taxonomy ON (wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
WHERE (wp_term_relationships.object_id = wp_posts.ID)
AND wp_term_taxonomy.taxonomy = 'calificaciones'
)) THEN 'YES' ELSE 'NO' END as calificacion
FROM wp_posts
WHERE wp_posts.post_type = 'ultimas-noticias'
You can use select distinct
SELECT distinct wp_posts.post_title, IF (wp_term_taxonomy.taxonomy = 'calificaciones', wp_terms.slug, 'no') as calificacion
FROM wp_posts
JOIN wp_term_relationships ON (wp_term_relationships.object_id = wp_posts.ID)
JOIN wp_term_taxonomy ON (wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
AND wp_posts.post_type = 'ultimas-noticias'
#AND wp_term_taxonomy.taxonomy = 'calificaciones'
I'm trying to find out the most purchased products but to only count distinct users ids. Basically my client wants to stop duplicate purchases from the same user, so that they can't affect the chart/best sellers.
I need to count all order_items for that product, using only Distinct users ids. Currently the results are counting all order_items so the Distinct isn't working.
Any help and I would be grateful.
Thanks in advance
SELECT *
FROM
( SELECT DISTINCT
order_item_meta_3.meta_value as distinct_user_order_items_id,
order_item_meta_2.meta_value as product_id,
SUM( order_item_meta.meta_value ) as item_quantity
FROM
wp_woocommerce_order_items as order_items
LEFT JOIN wp_woocommerce_order_itemmeta as order_item_meta
ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN wp_woocommerce_order_itemmeta as order_item_meta_2
ON order_items.order_item_id = order_item_meta_2.order_item_id
LEFT JOIN wp_woocommerce_order_itemmeta as order_item_meta_3
ON order_items.order_item_id = order_item_meta_3.order_item_id
LEFT JOIN wp_posts AS posts
ON order_items.order_id = posts.ID
LEFT JOIN wp_term_relationships AS rel
ON posts.ID = rel.object_ID
LEFT JOIN wp_term_taxonomy AS tax
USING( term_taxonomy_id )
LEFT JOIN wp_terms AS term
USING( term_id )
WHERE
posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('completed','processing','on-hold')
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_qty'
AND order_item_meta_2.meta_key = '_product_id'
AND order_item_meta_3.meta_key = '_user_id'
GROUP BY
order_item_meta_2.meta_value
ORDER BY
item_quantity DESC ) as order_table,
wp_posts
LEFT JOIN wp_postmeta as mk1
ON wp_posts.ID = mk1.post_id
LEFT JOIN wp_postmeta as mk2
ON wp_posts.ID = mk2.post_id
WHERE
order_table.product_id = wp_posts.ID
AND wp_posts.ID = mk1.post_id
AND mk1.meta_key = 'is_album'
AND mk1.meta_value = 0
AND mk2.meta_key = '_price'
AND mk2.meta_value = 0
Wouldn't you need to do the sum outside the group by. you have to materialize the distinct set first then aggregate it...
so change first few lines to...
SELECT distinct_user_order_items_Id, product_Id, sum(item_Quantity) as item_Quantity
FROM (
SELECT
order_item_meta_3.meta_value as distinct_user_order_items_id,
order_item_meta_2.meta_value as product_id,
order_item_meta.meta_value as item_quantity