Mysql query unknown behavior - mysql

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?

Related

Difficulty with SQL and WPDB in particular in the WHERE clause

I'm Stumped
I have 1/2 a dozen queries that all return expected results in MySQL Workbench, however, return nothing when called from WPDB.
I have tested WPDB works with very simple select statements, however, when they get more difficult WPDB doesn't like what I'm feeding it. I have tried for 2 days before turning here to ask - sadly I get no errors from WPDB just no results.
The query below returns me a stock date that any future orders will be able to ship. If run through myself. But return empty arrays in WPDB.
I understand this SQL needs some work, needs to be run through prepare and should be using the WPDB prefix - All this tidy up will come if I can get it working.
I have found that by simply leaving off the where clause I get a result - so this is where I have been focussing. CAN ANYONE SEE WHAT I HAVE DONE WRONG? or MOST WRONG?
Select stocketa as NextDispatch From
(
Select ETA as stocketa,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH FROM
(
Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH
FROM wp_posts p
LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date')
LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id)
LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' )
LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' )
LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock')
Where OIM2.meta_value = 5734 and p.post_status='atum_pending'
Order By ETA ASC
) as q1
) as q2
WHERE q2.QtyInbound_rt > q2.InverseSOH
LIMIT 1
This is how I am attempting to run it with WPDB (My first attempt at use of WP and WPDB so ... please dont laugh
$ProdID = get_the_ID();
echo ("Prod ID:" . $ProdID);
if(!defined('DIEONDBERROR')) define( 'DIEONDBERROR', true );
global $wpdb;
$selectString ="
select ETA as inStockETA, QtyInbound_rt as QI,InverseSOH as ISOI FROM ( Select ETA,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH
FROM ( Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH FROM wp_posts p
LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date') LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id)
LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' )
LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' )
LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock')
Where OIM2.meta_value = 5734 and p.post_status='atum_pending' Order By ETA ASC
) as q1
) as q2
WHERE q2.QtyInbound_rt > q2.InverseSOH LIMIT 1
";
//$selectString = $wpdb->prepare($selectString,$ProdID);
echo("Select String:" . $selectString);
//$nextShip = $wpdb->get_results($selectString);
//$nextShip = $wpdb->get_results($selectString,ARRAY_A);
$wpdb->show_errors();
$nextShip = $wpdb->get_results($selectString);
$wpdb->print_error();
$wpdb->hide_errors();
After putting in DIEONDBERROR ()
The output is :
Prod ID:5734Select String: select ETA as inStockETA, QtyInbound_rt as QI,InverseSOH as ISOI FROM ( Select ETA,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH FROM ( Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date') LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id) LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' ) LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' ) LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock') Where OIM2.meta_value = 5734 and p.post_status='atum_pending' Order By ETA ASC ) as q1 ) as q2 WHERE q2.QtyInbound_rt > q2.InverseSOH LIMIT 1
WordPress database error: [] select ETA as inStockETA, QtyInbound_rt as QI,InverseSOH as ISOI FROM ( Select ETA,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH FROM ( Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date') LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id) LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' ) LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' ) LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock') Where OIM2.meta_value = 5734 and p.post_status='atum_pending' Order By ETA ASC ) as q1 ) as q2 WHERE q2.QtyInbound_rt > q2.InverseSOH LIMIT 1
To know what is happening exactly with your query, use the following piece of code:
$wpdb->show_errors();
$nextShip = $wpdb->get_results($selectString);
$wpdb->print_error();
$wpdb->hide_errors();
and let me know the generated output.
By the way, there are some syntax errors in your query:
You have entered JOI$ but the correct would be JOIN
There is an extra LEFT JOIN before the WHERE clause that should not be there.
Please, check the query you are assigning to the $selectString variable.

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

How convert rows to columns(SQL)?

I want to do this:
Desired Output
but I actually have this:
Current Output
this is my sql code:
enter code here
SELECT `a`.`itemId` AS itemId, `a`.`donationId` AS `donationId`,
`a`.`typeId` AS `typeId`, `a`.`currencyId` AS `currencyId`,
`a`.memberId AS memberId, `a`.amount_total AS amount_total, a.ref AS
ref, a.ref_bank AS ref_bank, `a`.`hidden` AS `hidden`, a.code AS code,
concat( `b`.`first_name`, ' ', `b`.`last_name` ) AS member_name,
`c`.currencyCode AS currencyCode, `f`.`name` AS `payment_name`,
e.itemEnvelopId, e.itemId AS itemIdP, e.accountId, e.amount,
e.hidden as envelopHidden, g.NAME, g.new_number
FROM
ai_donation_items `a` LEFT JOIN ai_church_members `b` ON a.memberId = b.memberId
LEFT JOIN ai_currency `c` ON a.currencyId = c.currencyId
LEFT JOIN ai_payment_types `f` ON a.typeId = f.typeId
LEFT JOIN ai_donation_envelops_item as e on a.itemId = e.itemId
LEFT JOIN ai_accounts AS g ON e.accountId = g.accountId
WHERE `a`.`hidden` = 0 AND e.hidden = 0 and a.donationId = 1

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 Update a table on these arrays?

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