Update several posts' attachment with SQL - mysql

I've tried in several different ways to write an SQL query that would insert/update post's image attachment, but I haven't succeeded. I'd like to change several posts' image to be the same.
In other words I'd like to update postmeta to value XXX where post's title contains certain string. The problem is that posts' titles are in posts table and not in the postmeta table so I would have to somehow get the title from posts using post's id.
This is what I've tried:
SELECT ID FROM wp_posts WHERE post_title LIKE ‘%*part of title*%’;
UPDATE wp_postmeta
SET meta_value = *attatchment-post’s id*
WHERE post_id IN (*post* *id’s which attachment is to be updated*) AND meta_key = ‘*my attachment field*’;
The second part works if I manually list all of the posts' id's. Basically id like to use the result of the SELECT query in the second UPDATE query.
Any ideas, thanks?

Doing it in the same line as you have written -
UPDATE wp_postmeta
SET meta_value = (select meta_value from wp_postmeta where post_id = *attatchment-post’s id* AND meta_key = '*my attachment*')
WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_title LIKE ‘%*part of title*%’;
) AND meta_key = '*my attachment*'

In MySQL you can use joins in the update statements as well, and lift the where criteria from the select statement over to combine it with the update's where criteria:
update wp_postmeta wpm
inner join wp_posts wp on wpm.post_id=wp.id
set wpm.meta_value=*attatchment-post’s id*
where wpm.meta_key = ‘*my attachment field*’ and wp.post_title LIKE ‘%*part of title*%’

Related

SQL Select statement to get all records that contains one or more values in a string

I am stuck with this problem:
I made 3 custom meta_key in wp_postmeta. Now I have to select all the records that contain in the column meta_value, one or more words, from a string I provide.
This is the SQL I wrote, but it's not giving me back any results, even if I have one word matching the REGEX.
SELECT post_id FROM wp_postmeta WHERE
meta_key = 'packaging_attributes'
AND
meta_key = 'properties_attributes'
AND
meta_key = 'tech_functions_attributes'
AND
meta_value REGEXP 'chased-floor|chased-wall-plasterboard|alarm-en|0-22|0-220-75|15|25'
This is the screenshot from the db:
As you can see in row 1008 I have chased-wall-plasterboard, that should match the above select, and give me back 39 as result. But it's not working.
If I remove the three meta_key = column_name I am going to get tons of results but not the 39.
I assume that what you really want are meta posts having records with all 3 meta values in the current WHERE clause. If so, then aggregation is one approach:
SELECT post_id
FROM wp_postmeta
WHERE meta_key IN ('packaging_attributes', 'properties_attributes', 'tech_functions_attributes') AND
meta_value REGEXP 'chased-floor|chased-wall-plasterboard|alarm-en|0-22|0-220-75|15|25'
GROUP BY post_id
HAVING COUNT(DISTINCT meta_key) = 3;
It is not clear whether the restriction on meta_value really belongs there.
You should use OR operation to gather all 4 rows.
SELECT post_id FROM wp_postmeta WHERE
meta_key = 'packaging_attributes'
OR
meta_key = 'properties_attributes'
OR
meta_key = 'tech_functions_attributes'
OR
meta_value REGEXP 'chased-floor|chased-wall-plasterboard|alarm-en|0-22|0-220-75|15|25'

How to Count Times an ID Appears in a Column in a Different Table

Apologies in advance: I'm sure this is relatively easy and has been asked ad nauseam, but I just can't quite come up with the proper search.
Basically, I'm trying to take a list of IDs queryed from one table, and then see which ones DO NOT appear in a separate column in another table.
It's a Wordpress MySQL database. Authors are attached to a Post via metadata. Both authors and posts are considered to be posts for database purposes. There is one table containing both posts/authors: wp_posts/wp. There is another table containing the metadata attached to a post/author: wp_postmeta/wm.
I'm attempting to take a list of authors from wp_posts and see which ones are orphans, i.e. not attached to a post, by checking a column called meta_value in wp_postmeta.
An Author is tied to a Post by having the Author's ID from wp_posts show up in the wm.meta_value column for a Post. But Authors themselves are declared by having an 'author' value in the same column. So an Author with an ID of 17078 will have an 'author' value in wm.meta_value, while a Post attributed to that Author will have 17078 in wm.meta_value.
The following query gets me about halfway there by returning all the Authors that we have in our database:
SELECT
post_title,
ID
FROM
wp_posts wp
JOIN
wp_postmeta wm
ON
wp.ID = wm.post_id
WHERE
wm.meta_value = 'author'
I need to somehow take that returned list and highlight which of those IDs do not show up in the wm.meta_value column for all the posts. Any suggestions or guidance would be greatly appreciated.
Do you need below -
SELECT ID
FROM wp_postmeta wm
WHERE wm.meta_value = 'author'
AND NOT EXISTS (SELECT 1
FROM wp_postmeta wm2
WHERE wm2.ID = wm.ID)
Try something like this (view on DB Fiddle):
WITH
authors AS (
SELECT post_id
FROM wp_postmeta wm
WHERE wm.meta_value = 'author'
),
posts AS (
SELECT post_id, (meta_value +0) AS author_id
FROM wp_postmeta wm
WHERE wm.meta_value <> 'author'
)
SELECT post_id
FROM authors
WHERE NOT EXISTS (
SELECT 1
FROM posts
WHERE authors.post_id = posts.author_id
);
The query that I eventually went with, thanks to the hints provided by the other answers, was:
SELECT
wm.post_id
FROM
wp_postmeta wm
WHERE
wm.meta_value = 'author'
AND NOT EXISTS (
SELECT 1
FROM wp_postmeta wm2
WHERE wm2.meta_key = '_author'
AND wm2.meta_value LIKE CONCAT('%"',wm.post_id,'"%')
)

Update Table Based on Existing ID with Inner Join & Update

I'm trying to pull data from an existing table (wp_postmeta) and insert the meta_value into the new table (cigar_flavor_scoring). The data should be based on the post_id and I only want to pull in data from the wp_postmeta table with a meta_key of 'caramel'. I want to then take that value and insert it into the cigar_flavor_scoring table, column caramel.
I feel like I'm very close.
SELECT meta_value
FROM wp_postmeta
UPDATE cigar_flavor_scoring
INNER JOIN cigar_flavor_scoring ON (wp_postmeta.post_id = cigar_flavor_scoring.post_id)
WHERE meta_key = "caramel"
SET cigar_flavor_scoring.caramel = wp_postmeta.meta_value
I guess you are looking to update a table using INNER JOIN.
Please check this thread - SQL Server - inner join when updating

Get Multiple rows in SQL Query

I'm using a SQL query in JDBC. It is supposed to get:
1- post_id (from table wp_posts) WHERE post_type = product
2- post_title (from table wp_posts) WHERE post_type = product
3- meta_value from table wp_postmeta WHERE (wp_posts.post_id = wp_postmeta.post_id) AND wp_postmeta.meta_key = _stock_status, total_sales, _regular_price, _sale_price, _price, _stock
(from specified META_KEYs from table WP_POSTMETA corresponding to my POST_IDs from table WP_POSTS)
Basically, I'm working on getting WooCommerce product details from WP Database.
This is the code my friend has helped me with so far:
SELECT wp_posts.ID, wp_posts.post_title,wp_postmeta.meta_value FROM wp_posts, wp_postmeta WHERE (wp_posts.ID = wp_postmeta.post_id) AND (wp_postmeta.meta_key='_stock')
Just like it is getting _stock at the end of the query, I want to get 5 more items (_stock_status, total_sales, _regular_price, _sale_price, _price) for each POST_ID
EDIT:
My friend helped me with another query, but it is not getting the information in a single array, instead it makes different array items for each piece of information.
SELECT wp_posts.ID, wp_posts.post_title,wp_postmeta.meta_key,wp_postmeta.meta_value FROM wp_posts, wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id HAVING wp_postmeta.meta_key='_stock' OR wp_postmeta.meta_key='_stock_status' OR wp_postmeta.meta_key='total_sales' OR wp_postmeta.meta_key='_regular_price' OR wp_postmeta.meta_key='_sale_price' OR wp_postmeta.meta_key='_price'
I want the results to come out in a single 2D array.
This select statement below follows the question that wants to include additional meta_key information (not just "_stock"). Change accordingly, as only you have your data.
The OP clearly has typos in this sentence "wp_postmeta.meta_key = _stock_status, total_sales, _reulgar_price, _sale_price, _price, _stock" as well as that query that his "friend" wrote.
SELECT p.ID, p.post_title,m.meta_value
FROM wp_posts p
join wp_postmeta m
on p.ID=m.post_id
AND m.meta_key in ('_stock','_stock_status','total_sales','_regular_price','_sale_price','_price')

How do I return a field in MySQL from a sub-query linked to the main query results?

I'm having a bit of a problem with a query I'm using on a wordpress database. The query below is returning the correct and expected data for meta_value but my problem comes from the fact I don't get a field returned for the original post_id from the subquery, so I'm not able to link a specific meta_value with the original post_id - I may well need to restructure this but I'm a bit lost as to how to return this data associated with the meta_value it found.
SELECT meta_value
FROM wp_postmeta
WHERE post_id IN (SELECT meta_value FROM wp_postmeta WHERE post_id IN ('1','2','3','4'))
AND meta_key = '_wp_attached_file'
Sample data
post_id meta_key meta_value
1 _thumbnail_id 2
2 _wp_attached_file image.jpg
So as an example, given a list of 1 or more post_ids ('1'), I find the meta_value ('2') and look for another entry with a matching post_id ('2') and specified meta_key ('_wp_attached_file'), and I need to return both the meta_value ('image.jpeg') and the original post_id ('1')
Thanks in advance
Just use a self join:
select wp1.*, wp2.*
from wp_postmeta wp1, wp_postmeta wp2
where wp2.post_id in (1,2,3)
and wp1.meta_value = wp2.post_id and wp2.meta_key = '_wp_attached_file';