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"
Related
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
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 retrieve some data from a Wordpress database:
SELECT M.meta_value,wp_users.ID
FROM wp_postmeta AS M
WHERE meta_key = "_from_email" AND post_id = 277124
LEFT JOIN wp_users ON M.meta_value = wp_users.user_email
Here, my intention is to get the ID of the wp_users user who has an email identical to one in meta_value.
But I get the following error:
1064 - 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 'LEFT JOIN wp_users ON M.meta_value = wp_users.user_email
Do you see any syntax error?
left join must be declare before where clause
SELECT M.meta_value,wp_users.ID
FROM wp_postmeta AS M
LEFT JOIN wp_users ON M.meta_value = wp_users.user_email
WHERE meta_key = "_from_email" AND post_id = 277124
be sure you are not using column for left joined table in where clause otherwise this implies that the join work as an inner join .. eventually move these column in the related ON clause
How do I rewrite the query below to avoid the error "You can't specify target table for update in FROM clause"?
UPDATE wp_postmeta
SET meta_value = meta_value + 'A' WHERE (SELECT post_title FROM wp_posts A
LEFT JOIN wp_postmeta B ON B.post_id = A.id WHERE A.post_type = 'player' AND
B.meta_key ='_yoast_wpseo_metadesc') = 'Eric Bledsoe'
This is a MySQL limitation. You can use a join instead. This is one guess on what you intend with your query:
UPDATE wp_postmeta pm JOIN
wp_posts p
ON pm.post_id = p.id AND
p.post_type = 'player' AND
pm.meta_key ='_yoast_wpseo_metadesc'
SET pm.meta_value = CONCAT(pm.meta_value, 'A')
WHERE p.post_title = 'Eric Bledsoe';
As mentioned in the comment, your query either updates all rows in wp_postmeta or none of them. The subquery has no correlation clause to the outer query.
I have to make a mysql select from a wordpress database :
SELECT wp_posts.ID, wp_posts.post_title AS post_title,
responsable.meta_value AS responsable,
nif_cliente.meta_value AS nif_cliente,
CONCAT_WS(' ',contactos_0_nombre.meta_value,CONCAT('<br><br>',contactos_1_nombre.meta_value)) AS contactos,
tipo_de_empresa.meta_value AS tipo_de_empresa,
tipo_cliente.meta_value AS tipo_cliente
FROM wp_posts
LEFT JOIN wp_postmeta AS responsable
ON wp_posts.ID = responsable.post_id AND responsable.meta_key='responsable'
LEFT JOIN wp_postmeta AS tipo_de_empresa
ON wp_posts.ID = tipo_de_empresa.post_id AND tipo_de_empresa.meta_key='tipo_de_empresa'
LEFT JOIN wp_postmeta AS nif_cliente
ON wp_posts.ID = nif_cliente.post_id AND nif_cliente.meta_key='nif_cliente'
LEFT JOIN wp_postmeta AS contactos_0_nombre
ON wp_posts.ID = contactos_0_nombre.post_id AND contactos_0_nombre.meta_key='contactos_0_nombre'
LEFT JOIN wp_postmeta AS contactos_1_nombre
ON wp_posts.ID = contactos_1_nombre.post_id AND contactos_1_nombre.meta_key='contactos_1_nombre'
LEFT JOIN wp_postmeta AS tipo_cliente
ON wp_posts.ID = tipo_cliente.post_id AND tipo_cliente.meta_key='tipo_cliente'
WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'clientes'"
I want to concate contactos_n_nombre. I can write it manual but 'n' it's dinamic (i don't know how much it is) It can be 0 if there are only one contact, but can be 1000 if this client have more contact methods.
How can i make it to search how many contactos have one client, and concate all of them
Thanks
If I understand your question correctly, you can use the MySQL GROUP_CONCAT function. You may need to adjust this for your situation, but the basic idea is:
Set up the database JOIN as:
LEFT JOIN wp_postmeta AS contactosdb ON wp_posts.ID = contactosdb.post_id
Then use the following in your SELECT:
GROUP_CONCAT(contactosdb.meta_value SEPARATOR '<br><br>') AS contactos