I'm trying to calculate the Yield based on Wordpress meta_values and I'm using the MySQL statement below, which is not working. The SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100) doesn't return any values.
Can someone help me with what's wrong with the expression below.
Thank you.
global $post;
$post_author = $post->post_author;
$post_status = 'publish';
$yield = $wpdb->get_var( $wpdb->prepare(
"
SELECT (ROUND((((SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100),2),2)
FROM {$wpdb->posts} p
JOIN {$wpdb->users} u ON p.post_author = u.ID
LEFT JOIN {$wpdb->postmeta} staked
ON p.ID = staked.post_id AND staked.meta_key = 'staked'
LEFT JOIN {$wpdb->postmeta} returned
ON p.ID = returned.post_id AND returned.meta_key = 'returned'
WHERE p.post_author = %s AND p.post_status = %s
",
$post_author, $post_status
) );
Your query doesn't make sense. The select statement is using the aliases balance and bet. The from clause has the aliases pm and posts.
Do you mean this?
SELECT (ROUND((((SUM(pm.meta_value+pm.meta_value)-SUM(pm.meta_value))/SUM(pm.meta_value))*100),2),2)
FROM {$wpdb->postmeta} pm INNER JOIN
{$wpdb->posts} p
ON pm.post_id = p.ID
WHERE pm.meta_key = %s AND pm.meta_key1 = %s AND p.post_author = %s AND
Month(p.post_date) = MONTH(CURRENT_DATE) AND p.post_status = %s
Related
I have the following query that bring the posts that contain a specific word in the title
SELECT posts_post.ID AS post_ID,
posts_post.post_date AS post_post_date,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post
FROM wp_posts AS posts_post
WHERE 1=1
AND posts_post.post_title LIKE '%HOTARAR%'
AND posts_post.post_type = 'post'
GROUP BY post_post_date
The problem now is that I need to bring the posts only from a specific category (tag slug for the category is hotarari-consiliu-local and has the ID 160), how could I modify the above query to bring posts only from a single blog posts category? Thanks!
Try the below query.
global $wpdb;
$make = $wpdb->get_results("
SELECT * FROM
$wpdb->posts
LEFT JOIN
$wpdb->term_relationships
ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN
$wpdb->term_taxonomy
ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.post_type = 'post'
AND
$wpdb->posts.post_title LIKE '%HOTARAR%'
AND
$wpdb->term_taxonomy.taxonomy = 'category'
AND
$wpdb->term_taxonomy.term_id = 160
ORDER BY
post_date DESC
");
Assuming Bhautik is on the right path, then something like this should work (I don't know where 'hotarari-consiliu-local' belongs in this).
SELECT p.ID post_ID
, p.post_date post_post_date
, CONCAT('',p.post_title,'') post_title_with_link_to_post
FROM wp_posts p
JOIN wp_term_relationships pt
ON pt.object_id = p.ID
JOIN wp_term_taxonomy t
ON t.term_taxonomy_id = pt.term_taxonomy_id
WHERE p.post_type = 'post'
AND p.post_title LIKE '%HOTARAR%'
AND t.taxonomy = 'category'
AND t.term_id = 160
ORDER
BY post_date DESC
I want to limit each user to register only one order per day.
To do this, I need to check each user's order before registering whether this user has any 'wp-processing' order or not?
I wrote below MySQL query in PHP, but it doesn't work.
I printed value of '$sql_count_from_status' and try it in phpmyadmin, SQL tab to find the error.
Value of the variable was:
SELECT count(ID) FROM wp_posts p LEFT JOIN wp_postmeta m ON p.ID=m.post_id WHERE p.post_status LIKE 'wc-processing' AND p.post_type LIKE 'shop_order' AND m.meta_key LIKE '_customer_user' AND m.meta_value LIKE '100577';
and showed me zero in phpmyadmin as result. Now I know the query has problem, because I had 'processing order' for user 100577.
function get_orders_count_from_status( $status , $this_user_id){
global $wpdb;
// We add 'wc-' prefix when is missing from order staus
$status = 'wc-' . str_replace('wc-', '', $status);
$sql_count_from_status = "
SELECT count(ID) FROM {$wpdb->prefix}posts p
LEFT JOIN {$wpdb->prefix}postmeta m
ON p.ID=m.post_id
WHERE p.post_status LIKE '$status' AND p.post_type LIKE 'shop_order'
AND m.meta_key LIKE '_customer_user'
AND m.meta_value LIKE '".$this_user_id."';";
return $wpdb->get_var($sql_count_from_status);
}
Here's an example of a plausible query (although counting on a LEFT JOIN is a bit strange):
SELECT count(*)
FROM {$wpdb->prefix}posts p
LEFT
JOIN {$wpdb->prefix}postmeta m
ON m.post_id = p.ID
AND m.meta_key = '_customer_user'
AND m.meta_value = :this_user_id
WHERE p.post_status LIKE :status
AND p.post_type = 'shop_order'
I am working on freelancing site on Wordpress, fetching data from post type 'projects' and when user select the projects then project bidder will show just have to update the bid form the freelancer (bidders)
$query_update=($wpdb->prepare("
update wp_postmeta pm
join wp_posts p
on p.id = pm.post_id
join wp_users u
on u.id = p.post_author
set pm.meta_value = '$bid'
where pm.meta_key = 'bid_budget'
and p.post_title = '$project'
and u.user_login = '$user'
"));
if($query_update > 0){
echo "Successfully Updated";
}
else{
echo "Error ! Wrong Query";
echo 'Project '.$project.'<br>';
echo 'Bid '.$bid.'<br>';
echo 'User '.$user;
}
The data in the mysql backend is not changing. Is there something wrong with my query?
Thanks I got the solution I am using the wrong query - the right one is as follows:
$query_update=$wpdb->query($wpdb->prepare("update wp_postmeta pm inner join wp_posts p on p.id = pm.post_id inner join wp_users u on u.id=p.post_author set pm.meta_value = '$bid' where pm.meta_key = 'bid_budget' and p.post_title='$project' and u.user_login='$user'"));
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.
this code displays the tags from current category only but, it gets all the tags (hundreds) so, i need to limit the number of returned results and make em random.
How to make this query get only 20 results randomly ?
/* Retrieve all tags from posts in selected categories */
$cats = array('beaches','mountains'); // Must be an array even if only one category
$cats_string = "'" . implode($cats,"','") . "'";
$sql = <<<EOSQL
SELECT DISTINCT t.*
FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'post_tag')
JOIN $wpdb->terms t ON tt.term_id = t.term_id
WHERE
p.ID IN (
SELECT p2.ID
FROM $wpdb->posts p2
JOIN $wpdb->term_relationships tr2 ON p2.ID = tr2.object_id
JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category')
JOIN $wpdb->terms t2 ON (tt2.term_id = t2.term_id AND t2.name IN ($cats_string))
WHERE p2.post_type = 'post'
AND p2.post_status = 'publish'
AND p2.post_date <= NOW()
)
EOSQL;
$terms = $wpdb->get_results($sql);
// print_r($terms);
echo "<br />";
foreach ($terms as $term) {
echo "ID:$term->term_id NAME:$term->name SLUG:$term->slug<br />";
}
Thanks
You can try an ORDER BY RAND() LIMIT 20, depending on your table size this can run in decent times. See here some details on when to avoid the order by rand() logic. Like suggested, in the specified post, the other approach is to retrieve all the entries and randomly select 20 entries, in PHP rather then using mysql.