How do I combine 2 SELECT statements where the result - mysql

How do I combine 2 SELECT statements where the result of the first select is used in the WHERE of the second SELECT
Below is the code I am using right now:
$order_id = 7655;
$first = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM wp_postmeta WHERE meta_key = '_ticket_order' AND meta_value = %d", $order_id ) );
if ( $first ) {
$second = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_key = '_ticket_event' AND post_id = %d", $first ) );
}
echo $second;

You could try using a join between the two queries
$second = $wpdb->get_var( $wpdb->prepare("SELECT b.meta_value
FROM wp_postmeta a
INNER JOIN wp_postmeta b ON a.post_id = b.post_id
WHERE a.meta_key = '_ticket_order' AND a.meta_value = %d", $order_id ) );

Related

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;

SQL - How to concatenate this table?

By running this SELECT query:
SELECT wp_posts.ID, wp_postmeta.meta_key, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_date < NOW()
AND wp_postmeta.meta_key = 'wpcf-lat'
OR wp_postmeta.meta_key = 'wpcf-long'
I get table like this:
id meta_key meta_value
------------------------------
1270 wpcf-lat 12.6589
1270 wpcf-long 78.7425
1658 wpcf-lat 22.3654
1658 wpcf-long 65.2985
But I need result table to be like this
id wpcf-lat wpcf-long
------------------------------
1270 12.6589 78.7425
1658 22.3654 65.2985
How can I accomplish that?
For a known set of meta_key you can use the following query
select
wp.ID,
max(
case when pm.meta_key = 'wpcf-lat' then pm.meta_value end
) as `meta_value`,
max(
case when pm.meta_key = 'wpcf-long' then pm.meta_value end
) as `wpcf-long`
from wp_posts wp
join wp_postmeta pm on pm.post_id = wp.ID
group by wp.ID ;
A simple while or foreach in your PHP code is the easiest way to put the data in the format you need:
$query = '...';
$resultset = $DB->query($query);
$list = array();
while ($row = $resultset->fetchArray()) {
// Check if the entry having this ID was created before
$id = $row['id'];
if (! isset($list[$id]) {
// Create a new entry
$list[$id] = array(
'id' => $id,
'wpcf-lat' => NULL,
'wpcf-long' => NULL,
);
}
// Update the corresponding property
$key = $row['meta_key'];
$list[$id][$key] = $row['meta_value'];
}
As koushik veldanda said, you can need to pivot the table.
Something like:
SELECT wp_posts.ID,
CASE WHEN wp_postmeta.meta_key = 'wpcf-lat' THEN wp_postmeta.meta_value END AS wpcf-lat,
CASE WHEN wp_postmeta.meta_key = 'wpcf-long' THEN wp_postmeta.meta_value END AS wpcf-long
FROM wp_posts
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_date < NOW()
AND wp_postmeta.meta_key = 'wpcf-lat'
OR wp_postmeta.meta_key = 'wpcf-long'
GROUP BY wp_posts.ID
I haven't tested this but it should be quite close.

MySQL LIKE operator behaving like = operator

It seems MySQL's LIKE operator behaves like a = operator.
The following MySQL query returns the expected result (1 entry):
$meta_key = '_locality';
$meta_value = 'The Hague';
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value LIKE %s
",
$meta_key,
$meta_value
) );
But the following, with only part of the original meta_value, returns an empty array:
$meta_key = '_locality';
$meta_value = 'The';
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value LIKE %s
",
$meta_key,
$meta_value
) );
What am I doing wrong here?
Please read mysql pattern matching syntax carefully: http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html . You lose '%' for LIKE matching:
$meta_key = '_locality';
$meta_value = '%The Hague%';
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value LIKE %s
",
$meta_key,
$meta_value
) );

Wordpress SQL - Multiple WHERE clauses

Ok it has been a while since I last wrote an sql query. I have the following query
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = 'select_team_leader'
AND {$wpdb->usermeta}.meta_value = {$user_leader})",
$user_search->query_where
);
I want to add an OR statement like
OR {$wpdp->usermeta}.user_id = {$user_leader}
Do I need to use an inner join for this ? thanks
No you don't need to use an extra join you just have to organize your query to look for either user id or meta_value
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = 'select_team_leader'
AND
(
{$wpdb->usermeta}.meta_value = {$user_leader}
OR {$wpdp->usermeta}.user_id = {$user_leader}
)
)",
$user_search->query_where
);
So above query will look for meta_value is equal to provided value ie. {$user_leader} or user_id is equal to {$user_leader} but whatever matches meta_key should be select_team_leader
Edit from comments
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE (
{$wpdb->usermeta}.meta_key = 'select_team_leader'
AND {$wpdb->usermeta}.meta_value = {$user_leader}
)
OR {$wpdp->usermeta}.user_id = {$user_leader}
)",
$user_search->query_where
);

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