MySQL Update a table on these arrays? - mysql

I have a query like so:
SELECT DISTINCT(wp.`ID`), wps.sku AS sku
FROM `wp_posts` AS wp
INNER JOIN `wp_postmeta` AS wpm ON (wpm.`post_id` = wp.`ID` AND wpm.`meta_key` = '_sku' AND wpm.`meta_value` = '')
INNER JOIN `wp_product_skus` AS wps ON (wps.`id_product` = wpm.`post_id`)
WHERE wp.`post_parent` = 0 AND wp.`post_type` = 'product' AND wp.`ID` NOT IN (SELECT post_parent FROM wp_posts WHERE post_parent != 0 AND post_type = 'product_variation')
It returns output like so:
I need to perform an UPDATE on all IDs that match in the wp_postmeta table with the post_id = ID and update the meta_value of sku with the value of the sku from the above pic.
How can I do this all from within a MySQL Query?

You want to perform a UPDATE with JOIN to other tables as needed like
UPDATE `wp_posts` wp
INNER JOIN `wp_postmeta` wpm ON wpm.`post_id` = wp.`ID`
AND wpm.`meta_key` = '_sku'
AND wpm.`meta_value` = ''
INNER JOIN `wp_product_skus` wps ON wps.`id_product` = wpm.`post_id`
WHERE wp.`post_parent` = 0
AND wp.`post_type` = 'product'
AND wp.`ID` NOT IN (
SELECT post_parent FROM wp_posts
WHERE post_parent != 0
AND post_type = 'product_variation'
)
SET wp.sku = wps.sku
(OR) By directly joining with your SELECT result set like below
UPDATE `wp_posts` wp
JOIN
(
SELECT DISTINCT wp.`ID`,
wps.sku AS sku
FROM `wp_posts` wp
INNER JOIN `wp_postmeta` AS wpm ON wpm.`post_id` = wp.`ID`
AND wpm.`meta_key` = '_sku'
AND wpm.`meta_value` = ''
INNER JOIN `wp_product_skus` wps ON wps.`id_product` = wpm.`post_id`
WHERE wp.`post_parent` = 0
AND wp.`post_type` = 'product'
AND wp.`ID` NOT IN (
SELECT post_parent FROM wp_posts
WHERE post_parent != 0
AND post_type = 'product_variation')
) TAB ON wp.ID = TAB.ID
SET wp.sku = TAB.sku

Related

MySQL - Calculate a row value based on another row value in the same table

I have 2 values:
Column 1: meta_key(_regular_price), meta_value(double)
Column 2: meta_key(_specific_country_regular_price), meta_value(double)
I want to copy the meta_value from column 2 to column 1 multiplied by the currency of the day. I'm trying without the multiplication first.
I've tried a lot of approaches, this is the last one:
-- 1. start a new transaction
START TRANSACTION;
UPDATE
`_postmeta_test`
SET
`meta_value` = (
SELECT
t2.meta_value
FROM
`_postmeta_test` t1
JOIN `_postmeta_test` t2
ON t1.meta_id = t2.meta_id
WHERE `meta_key` = '_specific_country_regular_price' )
WHERE
meta_key = '_regular_price'
AND `post_id` IN (
SELECT
`ID`
FROM
`_posts`
WHERE
`post_type` = 'product'
AND `post_status` = 'publish'
);
ROLLBACK;
It's not working and I'm exhausted. Can anyone help?
Use joins in the UPDATE query, no need for all those subqueries.
UPDATE _postmeta_test AS t1
JOIN _posts AS p ON t1.post_id = p.ID
JOIN _postmeta_test AS t2 ON t1.post_id = t2.post_id
SET t1.meta_value = t2.meta_value * #currency_of_the_day
WHERE t1.meta_key = '_regular_price'
AND t2.meta_key = '_specific_country_regular_price'
AND p.post_type = 'product' AND p.post_status = 'publish'

How to bulk update stock for not updated products

I need to be able change Stock levels to "Out of stock" or stock quanity to 0 to products that hasn't been updated.
So far i made 2 separate codes to update stock and to delete not-updated products, but i need to combine them
DELETE FROM `wp_posts` WHERE `post_modified` < "2019-05-14"
UPDATE wp_postmeta SET meta_value = 'outofstock' WHERE meta_value = 'instock' AND meta_key = '_stock_status'
Incidentally, your query (incorrectly posted as a comment) can be rewritten as follows:
UPDATE wp_postmeta x
JOIN wp_posts y
ON y.id = x.post_id
SET x.meta_value = 'outofstock'
WHERE x.meta_value = 'instock'
AND x.meta_key = '_stock_status'
AND y.post_type = 'product'
AND y.post_status = 'publish'
AND y.post_modified < "2019-05-14"

alternative to nested select mysql

I am running a query at the moment, and I believe that that nest selected is creating a bottle neck, is there an alternative option that I could use?
This is my query,
SELECT post_id,
post_order,
post_parent,
post_recycle,
post_status,
post_ps_id,
post_v_id,
post_src,
post_sn_id,
post_qpc_id,
post_date_posted,
post_scheduled_local_datetime,
post_recycle_repeats,
post_recycle_expiry_date,
post_text,
post_v_title,
link_url,
link_preview_removed,
link_name,
link_description,
link_caption,
link_url_is_bitlink,
link_bitly_destination_url,
link_expanded_url,
link_initial_is_bitlink,
link_destination,
link_picture,
link_picture_size,
link_facebook_image,
link_facebook_title,
link_facebook_description,
link_facebook_caption,
link_twitter_card,
link_twitter_image,
link_twitter_title,
link_twitter_description,
pause_m_id,
qpca_evergreen_too_frequent,
sn_network,
qpc_name,
qpc_colour,
ps_m_id,
ps_filename,
ps_via,
ps_s3,
ps_width,
ps_height,
ps_gif,
video.*,
(
SELECT COUNT(*) FROM post post_inner
WHERE (post_inner.post_parent = post.post_parent)
AND post_inner.post_status = 'published'
)
AS total_repeats
FROM post
JOIN social_network ON sn_id = post_sn_id AND sn_status = 'active'
JOIN queue_post_cat ON qpc_id = post_qpc_id
LEFT JOIN queue_post_cat_account ON qpca_qpc_id = post_qpc_id AND qpca_sn_id = post_sn_id
LEFT JOIN link ON link_id = post_link_id
LEFT JOIN pause ON pause_m_id = qpc_m_id AND pause_qpc_id = post_qpc_id AND pause_sn_id = post_sn_id
LEFT JOIN photo_status ON ps_id = post_ps_id
LEFT JOIN video ON post_v_id = v_id AND v_transcoded = 1 LEFT JOIN facebook ON fb_db_id = sn_account_id AND sn_network = 'facebook'
WHERE post_status != 'now'
AND post_m_id = 1
AND qpca_sn_id IS NOT NULL
AND qpca_qpc_id IS NOT NULL
AND post_status = 'queue' AND (sn_network = 'facebook'
OR sn_network = 'instagram' OR sn_network = 'twitter')
AND qpc_m_id = 1 AND (fb_type IS NULL OR fb_type != 'profile') ORDER BY post_order ASC
I believe the bottle neck is happening here,
SELECT COUNT(*) FROM post post_inner
WHERE (post_inner.post_parent = post.post_parent)
AND post_inner.post_status = 'published'
which is select within the main select, is there something I could do that would run faster?
You can try with subquery:
select * from
(
SELECT post_id,
post_order,
post_parent,
post_recycle,
post_status,
post_ps_id,
post_v_id,
post_src,
post_sn_id,
post_qpc_id,
post_date_posted,
post_scheduled_local_datetime,
post_recycle_repeats,
post_recycle_expiry_date,
post_text,
post_v_title,
link_url,
link_preview_removed,
link_name,
link_description,
link_caption,
link_url_is_bitlink,
link_bitly_destination_url,
link_expanded_url,
link_initial_is_bitlink,
link_destination,
link_picture,
link_picture_size,
link_facebook_image,
link_facebook_title,
link_facebook_description,
link_facebook_caption,
link_twitter_card,
link_twitter_image,
link_twitter_title,
link_twitter_description,
pause_m_id,
qpca_evergreen_too_frequent,
sn_network,
qpc_name,
qpc_colour,
ps_m_id,
ps_filename,
ps_via,
ps_s3,
ps_width,
ps_height,
ps_gif,
video.*,
FROM post
JOIN social_network ON sn_id = post_sn_id AND sn_status = 'active'
JOIN queue_post_cat ON qpc_id = post_qpc_id
LEFT JOIN queue_post_cat_account ON qpca_qpc_id = post_qpc_id AND qpca_sn_id = post_sn_id
LEFT JOIN link ON link_id = post_link_id
LEFT JOIN pause ON pause_m_id = qpc_m_id AND pause_qpc_id = post_qpc_id AND pause_sn_id = post_sn_id
LEFT JOIN photo_status ON ps_id = post_ps_id
LEFT JOIN video ON post_v_id = v_id AND v_transcoded = 1 LEFT JOIN facebook ON fb_db_id = sn_account_id AND sn_network = 'facebook'
WHERE post_status != 'now'
AND post_m_id = 1
AND qpca_sn_id IS NOT NULL
AND qpca_qpc_id IS NOT NULL
AND post_status = 'queue' AND (sn_network = 'facebook'
OR sn_network = 'instagram' OR sn_network = 'twitter')
AND qpc_m_id = 1 AND (fb_type IS NULL OR fb_type != 'profile')) A
inner join
(
SELECT post_parent ,COUNT(*) FROM post where post_status = 'published' group by post_parent
) B on A.post_parent = B.post_parent
ORDER BY post_order ASC

WooCommerce bulk change product type with mysql

When importing products with WpAllImport, all of our products are stored as variable products, even if they don't have any variations. We need these to be stored as single products.
How can we change all the product type to single for all products that doesn't have any variations using MySql? We're having trouble with the query.
Hoping some WooCommerce experts can help us out with this one..
Thank you very much!
First from MySQL take back up of your database, then you can get id of your product type from below query:
select term_id from wp_terms where name='variable' // assume return 4 as result
select term_id from wp_terms where name='simple' // assume return 2 as result
By above query you can get id of both product type.which need to use in update query mentioned below.
UPDATE wp_term_relationships
INNER JOIN wp_terms ON (wp_term_relationships.term_taxonomy_id = wp_terms.term_id)
SET wp_term_relationships.term_taxonomy_id = '2' where wp_term_relationships.term_taxonomy_id = '4'
First get all variable products with a loop. Using each product post Id, get the available variations. If there are no variations for the product. Then change each to simple.
<?php
$args = array(
'post_type' => 'product',
'product_type' => 'variable'
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) {
while ( $products->have_posts() ) : $products->the_post();
$variations = array();
$id = the_ID();
$args = array(
'post_parent' => $id
);
$variations = get_children( $args );
if(sizeOf($variations) == 0){
wp_set_object_terms( $id, 'simple', 'product_type' );
}
endwhile;
}
?>
-- update WooCommerce variable products without variations to simple
--1
CREATE TABLE PPCD_EDI_TEMP_UPDATE
SELECT r.object_id
FROM wp_posts wp
LEFT JOIN wp_term_relationships r ON wp.ID = r.object_id
LEFT JOIN wp_postmeta SKU on SKU.post_id = wp.ID and SKU.meta_key = '_sku' -- SKU.meta_value as sku
LEFT JOIN wp_postmeta PRICE on PRICE.post_id = wp.ID and PRICE.meta_key = '_price' -- PRICE.meta_value as price
LEFT JOIN wp_wc_product_custom_lookup PCL on PCL.product_id = wp.ID
LEFT JOIN wp_term_taxonomy tt ON r.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_posts wpv ON wp.id = wpv.post_parent AND wpv.post_type != 'attachment'
WHERE tt.taxonomy = 'product_type'
AND t.name = 'variable'
and wpv.ID is null; --test for one product, replace ; with the following: and SKU.meta_value = '10001894';
--2
UPDATE wp_term_relationships
SET term_taxonomy_id = (select term_id from wp_terms where name='simple')
WHERE term_taxonomy_id = (select term_id from wp_terms where name='variable') AND object_id IN (select object_id from PPCD_EDI_TEMP_UPDATE);
--test
SELECT SKU.meta_value as sku, PRICE.meta_value as price,
wp.id AS 'Product Id',
wpv.id AS 'Variant Id',
wp.post_title as parent_title,
wpv.post_title as variant_title,
wpv.post_excerpt
FROM wp_posts wp
LEFT JOIN wp_term_relationships r ON wp.ID = r.object_id
LEFT JOIN wp_postmeta SKU on SKU.post_id = wp.ID and SKU.meta_key = '_sku' -- SKU.meta_value as sku
LEFT JOIN wp_postmeta PRICE on PRICE.post_id = wp.ID and PRICE.meta_key = '_price' -- PRICE.meta_value as price
LEFT JOIN wp_wc_product_custom_lookup PCL on PCL.product_id = wp.ID
LEFT JOIN wp_term_taxonomy tt ON r.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_posts wpv ON wp.id = wpv.post_parent AND wpv.post_type != 'attachment'
WHERE tt.taxonomy = 'product_type'
AND t.name = 'variable'
and wpv.ID is null;

Mysql query unknown behavior

I have the following query that I'm working on for a wordpress website:
Select
posts_tender.ID AS tender_ID,
tender_meta_user_ministry_tbl.meta_value,
CONCAT('<a style="font-weight: bold;font-size: 15px;" href="'
,posts_tender.guid
,'" target="_blank">المزيد...</a>') AS tender_title_with_link_to_post,
posts_tender.post_status AS tender_post_status,
DATE_FORMAT(STR_TO_DATE(tender_meta_tender_date_tbl.meta_value , '%Y-%m-%dT%H:%i'), '%d/%m/%Y') AS tender_meta_tender_date,
posts_tender.post_title AS tender_meta_tender_subject,
tender_taxonomy_language_tbl.name AS tender_taxonomy_language,
tender_taxonomy_tendercategory_tbl.name AS tender_taxonomy_tendercategory,
tender_meta_tender_status_tbl.meta_value AS tender_meta_tender_status
FROM wp_posts AS posts_tender
LEFT OUTER JOIN
(SELECT tender_meta_tender_date_tbl_posts.ID as id,
meta_value,
meta_key
FROM wp_postmeta AS tender_meta_tender_date_tbl_postmeta
INNER JOIN wp_posts AS tender_meta_tender_date_tbl_posts
ON tender_meta_tender_date_tbl_postmeta.post_id = tender_meta_tender_date_tbl_posts.ID
AND tender_meta_tender_date_tbl_posts.post_type = 'tender'
) AS tender_meta_tender_date_tbl
ON tender_meta_tender_date_tbl.meta_key = 'tender_date'
AND tender_meta_tender_date_tbl.id = posts_tender.ID
LEFT OUTER JOIN
(SELECT tender_meta_user_ministry_tbl_posts.ID as id,
meta_value,
meta_key
FROM wp_postmeta AS tender_meta_user_ministry_tbl_postmeta
INNER JOIN wp_posts AS tender_meta_user_ministry_tbl_posts
ON tender_meta_user_ministry_tbl_postmeta.post_id = tender_meta_user_ministry_tbl_posts.ID
AND tender_meta_user_ministry_tbl_posts.post_type = 'tender'
) AS tender_meta_user_ministry_tbl
ON tender_meta_user_ministry_tbl.meta_key = 'user_ministry'
AND tender_meta_user_ministry_tbl.id = posts_tender.ID
LEFT OUTER JOIN
(SELECT name,
object_id as id
FROM wp_terms AS tender_taxonomy_language_tbl_terms
INNER JOIN wp_term_taxonomy AS tender_taxonomy_language_tbl_termtaxonomy
ON tender_taxonomy_language_tbl_termtaxonomy.term_id = tender_taxonomy_language_tbl_terms.term_id
AND tender_taxonomy_language_tbl_termtaxonomy.taxonomy = 'language'
INNER JOIN wp_term_relationships AS rel_tender_taxonomy_language_tbl
ON tender_taxonomy_language_tbl_termtaxonomy.term_taxonomy_id = rel_tender_taxonomy_language_tbl.term_taxonomy_id
) AS tender_taxonomy_language_tbl
ON tender_taxonomy_language_tbl.ID = posts_tender.id
LEFT OUTER JOIN
(SELECT name,
object_id as id,
tender_taxonomy_tendercategory_tbl_terms.term_id AS tender_category_id
FROM wp_terms AS tender_taxonomy_tendercategory_tbl_terms
INNER JOIN wp_term_taxonomy AS tender_taxonomy_tendercategory_tbl_termtaxonomy
ON tender_taxonomy_tendercategory_tbl_termtaxonomy.term_id = tender_taxonomy_tendercategory_tbl_terms.term_id
AND tender_taxonomy_tendercategory_tbl_termtaxonomy.taxonomy = 'tendercategory'
INNER JOIN wp_term_relationships AS rel_tender_taxonomy_tendercategory_tbl
ON tender_taxonomy_tendercategory_tbl_termtaxonomy.term_taxonomy_id = rel_tender_taxonomy_tendercategory_tbl.term_taxonomy_id
) AS tender_taxonomy_tendercategory_tbl
ON tender_taxonomy_tendercategory_tbl.ID = posts_tender.id
LEFT OUTER JOIN
(SELECT tender_meta_tender_status_tbl_posts.ID as id,
meta_value,
meta_key
FROM wp_postmeta AS tender_meta_tender_status_tbl_postmeta
INNER JOIN wp_posts AS tender_meta_tender_status_tbl_posts
ON tender_meta_tender_status_tbl_postmeta.post_id = tender_meta_tender_status_tbl_posts.ID
AND tender_meta_tender_status_tbl_posts.post_type = 'tender'
) AS tender_meta_tender_status_tbl
ON tender_meta_tender_status_tbl.meta_key = 'tender_status'
AND tender_meta_tender_status_tbl.id = posts_tender.ID
where 1=1
AND posts_tender.post_type = 'tender'
And posts_tender.post_status = 'publish'
And tender_taxonomy_language_tbl.name = 'العربية'
And (tender_meta_tender_status_tbl.meta_value is NULL
OR tender_meta_tender_status_tbl.meta_value = 0)
And SUBSTRING_INDEX(
SUBSTRING_INDEX(
tender_meta_user_ministry_tbl.meta_value,
'\"',
-2),
'\"',
1) = Case
When (1103 = 0 Or 1103 = '' Or 1103= null Or 1103= 'null')
then 0
else 1103
End
and the query is working fine on my local database, but after importing the database on a live environment, the query is returning no data.
After troubleshooting the issue, if I added the below where condition to the query, a record will return:
and posts_tender.ID =14261
Does anyone have an idea on why is this happening?