Update Table from Another Table where column IN multiple values - mysql

My first attempt at this failed and I had to do a pesky restore on a really large table.
I've tried multiple queries, I think I am getting close, but everything I find provides a syntax error (I'm assuming because of the IN clause)
The tables are identical, but wp_postmeta_price only contains the value I want to restore _regular_price
Here's what the wp_postmeta table looks like
Lots of different columns here of various sorts.
The values in this table I'm trying restore I created another table wp_postmeta_prices with ONLY the meta_value of _regular_price I'm wanting to restore based from the meta_key and meta_id
Here's what that looks like
This may not be the easiest way, but what I did was export this table to an excel spreadsheet and used a tool to create the post_id column to a csv format so that I could use the IN clause for multiple unique instances
Here's maybe the closest I've gotten
UPDATE wp_postmeta
INNER JOIN wp_postmeta_price ON wp_postmeta.post_id = wp_postmeta_price.post_id
AND wp_postmeta.meta_id = wp_postmeta_price.meta_id
SET wp_postmeta.meta_value = wp_postmeta_price.meta_value WHERE '_regular_price' IN (2498,2443,2424,2518)
but this is not correct.
Can anyone please help me with the proper syntax, or enlighten me on a better possible solution for this?

Related

MySQL Join through an intermediary table

I am building a query and I need to select from the log table multiple columns, my issue is that i'm trying to find a way to select a column that has an FK in a table that has an FK to another table.
I have:
log.number_id,
numbers.number_id
numbers.country_id,
countries.country_id
Query is almost done, my only issue is that I need to show countries.country_id through an intermediary table FK numbers.country_id, I believe this is an INNER JOIN yet I have no idea how to create the concatenation, I searched google for this, yet I couldn't find something like a general formula of how to execute such an intermediary join.
I'm guessing you're looking for something like this.
Basically joining the table with both id's to the other tables on the common id.
SELECT log.*, ctry.*
FROM numbers AS ctrylog
JOIN log
ON log.number_id = ctrylog.number_id
JOIN countries AS ctry
ON ctry.country_id = ctrylog.country_id

Change meta_value relating to specific meta_key

I have a wordpress directory theme in which a bunch of listings are about to expire. The were all created on different days. I want to batch update the whole database so that all my listings are reset for another year as of today.
They related to the table wp_postmeta and the meta_key is "alive_days" and I need to update the pertaining meta_value.
If anyone can suggest an SQL query to fix this, I will be saved a from pulling out my hair.
Thanks in advance.
LLG
See http://snag.gy/KfJfB.jpg for screengrab of the database.
Based on your response to my question, this should do the trick (take a backup of your database first, just in case):
update wp_postmeta
set meta_value = '365' -- I assume you meant that, not 356
where meta_key = 'alive_days'
The existing blanks will be updated, but since they relate to posts that no longer exist, that shouldn't matter. If you don't want to update them, you could add an extra condition (I assume they contain the empty string, not null, but the ifnull should handle that):
update wp_postmeta
set meta_value = '365'
where meta_key = 'alive_days'
and ifnull(meta_value, '') != ''

MySQL how to select on multiple tables using Not Exist

I have three tables. One is a table of deletion candidates. This table was created with certain criteria, but did not include a couple of factors for consideration (limitations of the system). The other two tables were created considering those "left out" factors. So, I need to run a SELECT query on these three tables to come up with a deletion list.
What I started with is:
SELECT inactive.id
FROM inactive, renamed, returned
WHERE NOT EXISTS (inactive.id = remamed.id and inactive.id = returned.id)
But this is giving me an error. Can someone point out my error here?
Thank you
It's not entirely clear what you are trying to do here.
I assume you want a list of all rows from the inactive table that do not exist in either the renamed table or the inactive table. Is that right?
If so you can use a query like this:
SELECT inactive.id
FROM inactive
WHERE NOT EXISTS (select null from renamed where renamed.id = inactive.id)
AND NOT EXISTS (select null from returned where returned.id = inactive.id)

SQL Query to delete WP post using date range on a MySQL database

I am migrating a news aggregation WP site to a commercial service. We currently have over 14,000 posts on it.
We want to save the current database and reuse it under a different domain name for historical purposes.
Once we move to the new site we want to trim the WP database of all posts and related tables that are older than 01.01.2013
I know how to do a simple select where delete query.
But WP forum mods have told me that I should do an inner-join on the following tables to ensure I get everything cleaned up:
wp_postmeta
wp_term_relationships
wp_comments
wp_commentmeta
I am not familiar with inner-join. Can someone help me with this?
Not completely understanding the table structures involved, an INNER JOIN will join one table to another and return the records that match based on a certain criteria (usually joining two fields together such as a primary key and a foreign key).
To delete records from one table where some or all are in another table, the following syntax would be used:
DELETE TableName
FROM TableName
INNER JOIN AnotherTable ON TableName.id = AnotherTable.id
Here's a good visual representation of JOINS.

Is there a more efficient solution for this MySQL Query?

I am working on extracting and displaying data from a Wordpress DB to a mobile app for a customer and I am having a little trouble refining this query to be most efficient.
In wordpress, there are three tables that link the data I need to access
1. wp_posts - in this table there is the main post title, it's published status and the post type.
2. wp_postmeta - this table has all supplemental info related to the post id in the above table.
3. wp_p2p - this table has links to all the parent-child posts and their relationship.
Because of the volume of data in these tables, the query I currently have takes about 13 seconds to run, could you please take a look at this sqlfiddle and let me know what I could look at to improve it? The query in it's current form is not the end result, but improving it will improve my end result. I also need to add a search field on the "name" in the wp_postmeta table.
http://sqlfiddle.com/#!2/0e9e0/1
Any direction is appreciated, thank you!
If I understand correctly, you're looking for only child posts, in which case, the query below should be much faster:
SELECT wp_posts.id, post_title, post_status, post_type
FROM wp_posts
JOIN wp_postmeta ON (wp_posts.id = wp_postmeta.post_id)
LEFT JOIN wp_p2p ON (wp_posts.id = wp_p2p.p2p_from)
WHERE `post_status`='publish' AND `post_type`='merchant'
AND wp_p2p.p2p_from IS NULL
GROUP BY wp_posts.id
This query will be optimized to find where a match doesn't exist in the p2p table so that part will be much faster than how you're currently doing it. It looks like you can also remove the JOIN on wp_postmeta since you don't use it at all. Removing that JOIN would also make the GROUP BY redundant and removing it could help the performance a little. Removing the GROUP BY would also be a good practice since strictly you can't select non-aggregate fields that aren't in the GROUP BY clause, but MySQL provides for this functionality so the query will still work either way.
To begin with, you should join tables using INNER or OUTER JOIN syntax. So:
FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id
or perhaps you want an OUTER JOIN here?
Can you explain why you're doing a GROUP BY on wp_posts.id. Does this column not have unique values?