Update MySql table from complicated Select - mysql

I have complex SELECT request:
select meta_value
from wp_posts v
left join wp_postmeta pm on (pm.post_id = v.id)
left join wp_posts p on (v.post_parent = p.id)
where meta_key in ('_price','_regular_price')
and v.post_type = 'product_variation'
and p.id = '1743'
limit 0,100
It returns me 4 (four) needed fields with values like
400
500
300
350
I need to update these values and set their values equal, for example 1000.
Can I, based on my SELECT, run an UPDATE query?

Any condition used in a Select query could be used in Update query, of course it might need a little modification. What you can do in your case is the following:
meta_value
Update wp_posts
set meta_value = 1000
where id IN (select v.id
from wp_posts v
left join wp_postmeta pm on (pm.post_id = v.id)
left join wp_posts p on (v.post_parent = p.id)
where v.meta_key in ('_price','_regular_price')
and v.post_type = 'product_variation'
and p.id = '1743')
Please note that I didn't test this, so I am not totally sure it will work. Make sure to backup your database before any queries like this. Hopefully it will give you the desired results.
Edit: I have assumed that id in wp_posts is a unique value.

Related

How to delete entries from a SELECT query result in mysql?

I have the following SQL query :
SELECT wp_posts.* , wicl_translations.*
FROM wp_posts wp_posts join wp_icl_translations wicl_translations
ON (wicl_translations.element_id = wp_posts.ID)
WHERE (wicl_translations.language_code = 'es-es'
AND wicl_translations.element_type ='post_product'
AND wp_posts.post_type = 'product' ) GROUP BY wp_posts.ID
This returns all the results I need to delete from my database so I have tried several DELETE queries but getting syntax errors in all of them .
Example :
DELETE FROM wp_posts
WHERE (
SELECT wp_posts.* , wicl_translations.*
FROM wp_posts wp_posts join wp_icl_translations wicl_translations
ON (wicl_translations.element_id = wp_posts.ID)
WHERE (wicl_translations.language_code = 'pt-pt'
AND wicl_translations.element_type ='post_product'
AND wp_posts.post_type = 'product' ) GROUP BY wp_posts.ID
)
);
Also tried this :
DELETE FROM wp_posts WHERE wp_posts.ID = ANY IN (
SELECT wp_posts.ID, wicl_translations.*
FROM wp_posts wp_posts join wp_icl_translations wicl_translations
ON (wicl_translations.element_id = wp_posts.ID)
WHERE (wicl_translations.language_code = 'es-es'
AND wicl_translations.element_type ='post_product'
AND wp_posts.post_type = 'product' ) GROUP BY wp_posts.ID
)
It`s a complex aggregated query and I lack the mysql knowledge to properly write a rule for deleting these results .
How could I approach this ?
Thanks
If we have a complex query that returns the id value of rows in wp_posts that we want to delete (assuming that id is the primary key or a unique key of a row in the table)... as an example
SELECT p.id
FROM wp_posts p
JOIN wp_icl_translations t
ON t.element_id = p.id
WHERE t.language_code = 'es-es'
AND t.element_type = 'post_product'
AND p.post_type = 'product'
GROUP
BY p.id
We can then use that query as an inline view. We wrap the query in parens and reference it in the FROM clause of another query. MySQL requires that we assign an alias to thhe inline view (or derived table in the MySQL vernacular).
We can join the result from the inline view that back to the table we want to remove rows from. We write this a SELECT statement first
SELECT r.*
FROM ( -- inline view
SELECT p.id
FROM wp_posts p
JOIN wp_icl_translations t
ON t.element_id = p.id
WHERE t.language_code = 'es-es'
AND t.element_type = 'post_product'
AND p.post_type = 'product'
GROUP
BY p.id
) q
JOIN wp_posts r
ON r.id = q.id
to return the set of rows to be removed. We can verify that this is the intended set, or insert (create table as) the set of rows into backup...
Once we are confident that the SELECT is returning the rows we want to remove, we can convert it into a DELETE statement by replacing the SELECT keyword with DELETE.
DELETE r.*
FROM ( -- inline view
SELECT p.id
FROM wp_posts p
JOIN wp_icl_translations t
ON t.element_id = p.id
WHERE t.language_code = 'es-es'
AND t.element_type = 'post_product'
AND p.post_type = 'product'
GROUP
BY p.id
) q
JOIN wp_posts r
ON r.id = q.id
You'r on the right track !
You just miss the correct WHERE condition :
DELETE FROM wp_posts WHERE wp_posts.ids IN (...)
Make sure the result has only one column wich you shall refer to when deleting data from the targetted tables. The delete queries will be equal to the number of tables you will require to delete from ie.
DELETE FROM table_1 where common_column in (YOUR_SELECT_QUERY);
DELETE FROM table_2 where common_column in (YOUR_SELECT_QUERY);
DELETE FROM table_3 where common_column in (YOUR_SELECT_QUERY);
DELETE FROM table_nth where common_column in (YOUR_SELECT_QUERY);
Your select query be like,
SELECT GROUP_CONCAT(temp_tbl.ID) FROM (SELECT wp_posts.* , wicl_translations.*
FROM wp_posts wp_posts join wp_icl_translations wicl_translations
ON (wicl_translations.element_id = wp_posts.ID)
WHERE (wicl_translations.language_code = 'es-es'
AND wicl_translations.element_type ='post_product'
AND wp_posts.post_type = 'product' ) GROUP BY wp_posts.ID) AS temp_tbl

How to fix the error "You can't specify target table for update in FROM clause" when updating a SQL table

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.

Update column value by adding 2 column values together using a select an join query

I am looking for a way to update a 3rd column, by adding the values of 2 other columns together. The problem I am running into is that the UPDATE statement seems to need a table specified, but I am using a "virtual" table by doing SELECT and JOIN statements. Here is the code I currently have:
SELECT *
FROM wp_posts AS p
LEFT JOIN (
SELECT tr.object_id AS id,
t.name AS physical
FROM wp_term_relationships AS tr
INNER JOIN wp_term_taxonomy AS x
ON (x.taxonomy='pa_physical-inventory'
AND x.term_taxonomy_id=tr.term_taxonomy_id)
INNER JOIN wp_terms AS t
ON t.term_id=x.term_id
) AS mo ON p.id = mo.id
LEFT JOIN (
SELECT tr.object_id AS id,
t.name AS murphy
FROM wp_term_relationships AS tr
INNER JOIN wp_term_taxonomy AS x
ON (x.taxonomy='pa_murphy-inventory'
AND x.term_taxonomy_id=tr.term_taxonomy_id)
INNER JOIN wp_terms AS t
ON t.term_id=x.term_id
) AS pa ON p.id = pa.id
LEFT JOIN (
SELECT post_id AS id, meta_value AS totalinventory
FROM wp_postmeta
WHERE meta_key = '_stock'
) AS totalinventory ON p.id = totalinventory.id
WHERE p.post_status = 'publish'
AND p.post_type = 'product'
I am looking to add "murphy" and "physical" together and insert into "totalinventory" for each row that is returned. Some of these rows return "null" (like if "murphy" has no stock set it shows null) so I am looking to also take that as "0" when adding the values together.
Any help or guidance would be greatly appreciated as I have been scratching my head over this for far too long.
EDIT: I am open to PHP in the solution because eventually this will be run using a cron job.
In simpler cases, SELECTs can usually be converted to UPDATEs like so...
The SELECT:
SELECT *
FROM [tables and joins]
WHERE [conditions]
;
The UPDATE:
UPDATE [tables and joins (as above)]
SET tableA.fieldB = tableC.fieldD + tableE.fieldF
WHERE [conditions (same as above)]
;
Of course, if you have LEFT JOINs you'll need to compensate if necessary; I find it best to SELECT all the pieces you'll need for the SET first, before making the conversion to an UPDATE query.
Disclaimer: As I prefaced, depending on circumstances and the particulars of the data, this doesn't always work.

SQL show result if meta_key not exists or not is 1

with the following SQL query I check whether a meta value of a (Wordress) post is not equal to 1. But I also want to check if the meta_key 'gtp_conversion_uploaded' exists. And if it not exists I still want to select the ID.
SELECT DISTINCT p.ID
FROM wp_posts p
LEFT JOIN wp_postmeta m2 ON m2.post_id = p.ID
AND m2.meta_key = 'gtp_conversion_uploaded'
AND m2.meta_value != 1
So my query needs to do the following checks:
Select the results where meta_key 'gtp_conversion_uploaded' is not
equal to 1.
And also if the meta_key 'gtp_conversion_uploaded' not exists.
I tried it with a LEFT JOIN, but the problem is that if the meta_value of meta_key 'gtp_conversion_uploaded' is equal to 1, I still get results.
SELECT DISTINCT p.ID
FROM wp_posts p
LEFT JOIN wp_postmeta m2 ON m2.post_id = p.ID
AND m2.meta_key = 'gtp_conversion_uploaded'
WHERE m2.meta_key is null
OR m2.meta_value != 1

MySQL query 0 rows affected

in simple I try do that in PhpMyAdmin:
update wp_postmeta.meta_value = wp_posts.id where
wp_posts.post_name=articles.image
AND wp_posts.post_type=attachment
AND wp_postmeta.meta_key=_thumbnail_id
by this MySQL query:
update wp_postmeta m
join articles a on (m.post_id = a.id)
join wp_posts p on (m.post_id = p.ID)
set m.meta_value = p.ID
where p.post_name=a.image
AND a.image != NULL
AND p.post_type='attachment'
AND m.meta_key='_thumbnail_id'
it's work but 0 rows affected!
anyone can help?
Use subqueries with Andomar's answer. Using joins with update and delete statements is not good.
It should be something like
UPDATE wp_postmeta m SET m.meta_value = (SELECT p.ID FROM wp_posts p, articles a
WHERE m.post_id=p.ID AND m.post_id = a.id AND a.image IS NOT NULL
AND p.post_type='attachment' AND p.post_name=a.image)
WHERE m.meta_key='_thumbnail_id'
BACKUP FIRST! I didn't test this.
If doesn't work try to execute a select query if you can select them. If it doesn't select (and probably it won't) please check the structure of your query corresponding to your design.
SELECT m.meta_value, (SELECT p.ID FROM wp_posts p, articles a
WHERE m.post_id=p.ID AND m.post_id = a.id AND a.image IS NOT NULL
AND p.post_type='attachment' AND p.post_name=a.image) AS pID
FROM wp_postmeta m
WHERE m.meta_key='_thumbnail_id'
Also try your first query too
SELECT m.meta_value, p.ID AS pID FROM wp_postmeta m
INNER JOIN articles a on (m.post_id = a.id)
INNER JOIN wp_posts p on (m.post_id = p.ID)
WHERE p.post_name=a.image
AND a.image IS NOT NULL
AND p.post_type='attachment'
AND m.meta_key='_thumbnail_id'
Try to play with/modify the select queries to find out the problem.
This statement:
AND a.image != NULL
always evaluates to unknown. And where clause filters out rows which are not true. So a where filters out both unknown and false. Try this instead:
AND a.image is not null
See this wikipedia article for more information.