Database server version: Server version: 10.1.37-MariaDB - mariadb.org binary distribution
Error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '(partition by post_title order by wp_postmeta.meta_value) rn
' at line 8
Query:
-- Deleting all duplicate products in wp_posts table
DELETE FROM
wp_posts WHERE ID IN ( SELECT ID FROM
(
select ID,post_title,post_type,meta_value from
(SELECT wp_postmeta.post_id,post_title,post_type ,meta_value,
row_number()over(partition by post_title order by wp_postmeta.meta_value) rn
FROM wp_postmeta
JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
) t where t.rn <> 1
) AS aliasx
);
-- Deleting all corresponding wp_postmeta.post_ids that don't have a match in wp_posts.id after the duplicate deletion above
DELETE FROM wp_postmeta WHERE post_id IN (select post_id from (
SELECT pm.post_id
FROM
wp_postmeta pm
LEFT JOIN
wp_posts p
ON p.id = pm.post_id
WHERE
p.id IS NULL
AND
p.post_type = "Product"
) AS aliasy );
Could someone suggest a simple code fix that would ensure compatibility to the database version, 10.1.37-MariaDB?
db Fiddle here
Related
I spent some time in my WP DB trying to figure out how I can clean out some completed order data. Below is the query that I am confident will help remove order data from all of the different tables.
But when I run the following query:
DELETE * FROM wp_post
JOIN wp_postmeta ON wp_post.ID = wp_postmeta.post_id
JOIN wp_woocommerce_order_items ON wp_postmeta.post_id = wp_woocommerce_order_items.order_item_id
JOIN wp_woocommerce_order_itemmeta ON wp_postmeta.post_id = wp_woocommerce_order_itemmeta.order_item_id
WHERE wp_post.post_type = "shop_order"
AND wp_post.post_status = "wc-completed"
I get the following MySQL error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM wp_post
JOIN wp_postmeta ON wp_post.ID = wp_postmeta.post_id
JOIN wp_wo' at line 1
Am I not JOIN my WordPress Tables correctly?
This question may be more appropriate for stackoverflow but figured I would try here first.
Try adding the table name of the table you want delete rows
DELETE wp_post
FROM wp_post
JOIN wp_postmeta ON wp_post.ID = wp_postmeta.post_id
JOIN wp_woocommerce_order_items ON wp_postmeta.post_id = wp_woocommerce_order_items.order_item_id
JOIN wp_woocommerce_order_itemmeta ON wp_postmeta.post_id = wp_woocommerce_order_itemmeta.order_item_id
WHERE wp_post.post_type = "shop_order"
AND wp_post.post_status = "wc-completed"
and for deleting in more then a table add the tables name in DELETE clause
eg for wp_post and wp_postmeta use
DELETE wp_post, wp_postmeta
FROM wp_post
JOIN wp_postmeta ON wp_post.ID = wp_postmeta.post_id
JOIN wp_woocommerce_order_items ON wp_postmeta.post_id = wp_woocommerce_order_items.order_item_id
JOIN wp_woocommerce_order_itemmeta ON wp_postmeta.post_id = wp_woocommerce_order_itemmeta.order_item_id
WHERE wp_post.post_type = "shop_order"
AND wp_post.post_status = "wc-completed"
I'm trying to run this sql query and keeps getting this error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID WHERE
wp_posts.post_typ' at line 3 (Line 4)
My sql query is this:
UPDATE wp_postmeta
SET meta_value = "1316"
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_type="imagen_dia" AND wp_postmeta.meta_key="ae_post_template"
Any help please?
In mysql the JOIN should be before the set
UPDATE wp_postmeta
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID
SET meta_value = "1316"
WHERE wp_posts.post_type="imagen_dia" AND wp_postmeta.meta_key="ae_post_template"
The syntax for joined updates is vendor-specific. So do this without a join to have the update statement simple, readable and safe :-)
UPDATE wp_postmeta
SET meta_value = '1316'
WHERE meta_key = 'ae_post_template'
AND post_id IN (SELECT id FROM wp_posts WHERE post_type = 'imagen_dia');
This statement is standard SQL and should work in about every RDBMS.
You are missing the FROM word:
UPDATE wp_postmeta
SET meta_value = "1316"
FROM wp_postmeta
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_type="imagen_dia" AND wp_postmeta.meta_key="ae_post_template"
I'm trying to run this delete query in MYSQL (via phpMyAdmin) ANd I keep getting this error :
DELETE
p,pm
from wp_posts p
inner join wp_postmeta pm on pm.post_id = p.id
where p.id in (SELECT MIN( id ) AS min_id
FROM wp_posts inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id and meta_key = 'old_id')
WHERE post_type = 'post'
GROUP BY meta_value
HAVING COUNT( * ) > 1)
any idea why ?
Looking to your code
You don't should use as in subselect for IN clause
DELETE p, pm
from wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.id
where p.id in (SELECT MIN( id )
FROM wp_posts
inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id
and meta_key = 'old_id')
WHERE post_type = 'post'
GROUP BY meta_value
HAVING COUNT( * ) > 1)
I'm querying the WordPress wp_postmeta table for the lowest meta_value of rows with the meta_key item_thickness:
SELECT min(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key='item_thickness'
This works great.
Question: How would I extend this query to select the same lowest item_thickness from rows with the same post_id and with meta_key='item_status' and meta_value='Raw'
The post_id forms the relationship between these rows but I don't know how to do a JOIN on the same table or the proper syntax for a sub-query
This is my latest (failing) attempt at the query:
SELECT *
FROM wp_postmeta
JOIN (
SELECT min(cast(meta_value as unsigned)), post_id FROM wp_postmeta WHERE meta_key='item_thickness'
) b
ON wp_postmeta.post_id=b.post_id
I was able to use WP_Query to build the MySQL I needed and then edit it so it would select the value I wanted.
Working query.
$wpdb->get_var( "SELECT min(cast(wp_postmeta.meta_value as unsigned)) FROM wp_postmeta INNER JOIN wp_posts ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND ( wp_postmeta.meta_key = 'item_thickness' AND ( mt1.meta_key = 'item_status' AND CAST(mt1.meta_value AS CHAR) = 'raw' )) AND wp_posts.post_type = 'inventory' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')" );
I am working on a custom plugin in wordpress. I have a weird issue with one sql.
SQL:
SELECT SQL_CALC_FOUND_ROWS wp_posts. *
FROM wp_posts
INNER JOIN wp_term_relationships
ON ( wp_posts.ID = wp_term_relationships.object_id )
INNER JOIN wp_term_taxonomy
ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
WHERE 1 =1
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN ('23')
AND (
wp_posts.post_author =1
)
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'
)
GROUP BY wp_posts.ID
ORDER BY `wp_posts`.`as_stats_rating` DESC
LIMIT 0 , 30
Its returning the correct data but its not sorting results according to as_stats_rating.
I am stumped. Does anyone know what I am doing wrong?
Edit 1 : Update
Here is the structure of wp_posts:
Sample result:
ID as_stats_rating
1221 8
1222 10
All fields in sample results are :
ID post_author post_date post_date_gmt post_content post_title post_excerpt post_status comment_status ping_status post_password post_name to_ping pinged post_modified post_modified_gmt post_content_filtered post_parent guid menu_order post_type post_mime_type comment_count as_stats_numviews as_stats_numvotes as_stats_votestotal as_stats_rating
By the way, its not only about 'order by wp_posts.as_stats_rating', 'order by wp_posts.as_stats_numviews' have the same issue. (just to clear up, if you are wondering about as_stats_rating having varchar type)
Any chance that the as_stats_rating field is a string (char/text) datatype and not a numeric (float/int/decimal) datatype?