Replace mysql column data to onther column with Match ID - mysql

Hello I have a WordPress database just updated to my site to a new theme and my Product specification is missing in new theme I try to find and found new theme using a different table column to show the specification so I have to replace product specifications in the new column.
Following the original data
Table Name: wp_postmeta
SELECT
wp_postmeta.post_id,
wp_postmeta.meta_value,
wp_postmeta.meta_key
FROM
wp_postmeta
WHERE
wp_postmeta.meta_key = '_specifications'
ORDER BY
wp_postmeta.post_id
ASC
I want to replace above data in following table with matching ID
Table Name: wp_posts
SELECT
wp_posts.ID,
wp_posts.post_content
FROM
wp_posts
ORDER BY
wp_posts.ID
ASC

You can update the table wp_posts by joining it to your first query.
update wp_posts p inner join (
select post_id, meta_value
from wp_postmeta
where meta_key = '_specifications'
) m on m.post_id = p.id
set p.post_content = m.meta_value

Related

Grab Multiple Rows MYSQL Join

This applies to WordPress / Woocommerce
I'm using this query to grab a specific meta_value based matching post_ids in wp_postmeta and wp_posts
SELECT meta.meta_value
FROM wp_postmeta AS meta
JOIN wp_posts AS o
ON meta.post_id = o.ID
WHERE o.post_type = 'shop_order' AND o.post_status='wc-cancelled' AND meta.meta_key='_billing_email'
ORDER BY o.post_date DESC
My problem is I also want to get meta.meta_keys = '_billing_first_name' and '_billing_last_name' for those same conditions in the same query. Right now I have to get each row separately then combine them on an excel sheet.
Is there a way to do this?
If I understand correctly, a simple OR or using IN will do
SELECT meta.meta_value
FROM wp_postmeta AS meta
JOIN wp_posts AS o
ON meta.post_id = o.ID
WHERE o.post_type = 'shop_order' AND o.post_status='wc-cancelled'
AND meta.meta_key IN ('_billing_email' , '_billing_first_name', '_billing_last_name')
ORDER BY o.post_date DESC

how to use the "DELETE" in mysql with COUNT and INNER JOIN?

I'm starting in mysql, and I wonder how can I do to remove the result of the SELECT below, thank you all!
SELECT ID,post_name,meta_value, guid, COUNT( meta_value )
FROM wp_postmeta
INNER JOIN wp_posts
WHERE wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_value >100
GROUP BY meta_value
HAVING COUNT( meta_value ) >1
You can first select and create a array or you can directly put this query with delete query using IN clause.
DELETE from wp_posts
WHERE ID IN (
SELECT ID
FROM wp_posts
INNER JOIN wp_postmeta
WHERE wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_value >100
GROUP BY meta_value
HAVING COUNT( meta_value ) >1)
If you set foreign key with postmeta colum post_id as on delete cascade then data from this table will automatically remove.
Otherwise please consider to create unique array of post ids and postmeta ids. And use two separate delete query with IN clause.

combine two sql queries -

I've have two sql queries which I'm trying to combine
The first:
SELECT * FROM wp_posts
JOIN wp_postmeta on (post_id=ID)
WHERE meta_key = "packageID" and meta_value = 1
ORDER BY post_date limit 50
Joins the wordpress wp_post table to the wp_postmeta and gets all the posts meeting with packageID = 1 (I think it might be an inelegant way of doing it but it works)
The second
SELECT * FROM wp_postmeta
JOIN wp_posts ON (meta_value=ID)
WHERE post_id = 2110
AND meta_key = '_thumbnail_id'
again joins the wp_post table to the wp_postmeta table, so for the post with the id 2110 it successfully gets the thumbnail for that posts. NB 2110 is just an example of an id
In Wordpress a thumbnail is a kind of post. So in this example the text which constitutes post 2110 is a associated with post 2115 - the latter being the thumbnail
What I'm trying to do is get the list as in the first query but also get thumbnails associated with each post
I think I need two joins but I can't see how to do it (being an sql beginner)
NB this will be in a script outside Wordpress so I can't use Wordpress's built-in functions
You can try this one,if there are more than one thumbnails for the post you can get the list of thumbnails separated by comma
SELECT
*,
(SELECT
GROUP_CONCAT(meta_value)
FROM
wp_postmeta
WHERE post_id = wp.ID
AND wpm.meta_key = "_thumbnail_id") AS `thumbnails`
FROM
wp_posts wp
JOIN wp_postmeta wpm
ON (wpm.post_id = wp.ID)
WHERE wpm.meta_key = "packageID"
AND wpm.meta_value = 1
ORDER BY wp.post_date
LIMIT 50
Note : GROUP_CONCAT has a limit to concat characters but you
can increase this limit
To get only one thumbnail you can try this
SELECT
*,
(SELECT
(meta_value)
FROM
wp_postmeta
WHERE post_id = wp.ID
AND wpm.meta_key = "_thumbnail_id" LIMIT 1)
FROM
wp_posts wp
JOIN wp_postmeta wpm
ON (wpm.post_id = wp.ID)
WHERE wpm.meta_key = "packageID"
AND wpm.meta_value = 1
ORDER BY wp.post_date
LIMIT 50
try with the following code
SELECT * FROM wp_posts wp JOIN wp_postmeta wm on (wp.post_id=wm.ID) WHERE wp.meta_key = "packageID" and wp.meta_value = 1 ORDER BY wp.post_date limit 50;
use proper alias and try it.
Try using the post_type column. The attachments have a post_type of 'attachment'. I can further explain if needed.
Also the post to which the thumbnail is attached to will be in the column post_parent.
global $wpdb;
$query7 = "SELECT distinct wp_postmeta.meta_value, wp_postmeta.meta_key, wp_posts.ID
FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key = 'packageID'
AND wp_postmeta.meta_value = 1
AND (mt1.meta_key LIKE '_thumbnail_id')
$output = $wpdb->get_results( $query7 );
Use join with different aliases when joining same table more than once.
Hope this helps.
Try this
SELECT * FROM wp_posts P1
LEFT JOIN wp_postmeta M1 ON (M1.post_id=P1.ID)
WHERE (M1.meta_key = "packageID" and M1.meta_value = 1 )
LEFT JOIN wp_postmeta M2 ON (M2.meta_key=P1.ID AND M2.meta_key = '_thumbnail_id')
LEFT JOIN wp_posts P2 ON (M2.meta_value=P2.ID)
ORDER BY P1.post_date limit 50

Export WordPress Posts and Meta Information in CSV format using SQL Query

Apologies, this is a duplicate of entry of this post but I found I'm not getting the right answer there and not sure how to ask to move it over to SO?
I need to export all WordPress Posts and related Meta information using an SQL query in CSV format. I've managed to export just the Posts but realised that the Meta information is in another table.
Could someone please tell me how I would go about doing this please?
Thanks!
EDIT: I have already tried a number of plugins, none of which have worked for this specific scenario. I really do need to work on the query itself. Thanks
EDIT: The final outcome of the CSV file should look something like this. On one row, these are the columns that I will end up with. (I will delete the additional columns in CSV, I'm just giving a shortened version here)
post_content | post_title | **meta_key** | *meta_value* | **meta_key** | *meta_value* | **meta_key** | *meta_value* | **meta_key** | *meta_value* | **meta_key** | *meta_value*
So one post per row with the meta information in the same row.
EDIT: With the S-ha-dum, I've managed to get some way, but I have about 12 meta_value > meta_key values to pull out of the DB so I need help to write the rest of the query please
SELECT *,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Publisher' AND wp_postmeta.post_id = wp_posts.ID) as Publisher FROM wp_posts
A friend managed to help me with my solution in the end. This was his final SQL query to export all posts with meta information to a CSV file.
SELECT DISTINCT
post_title
, post_content
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (US\$)' AND wp_postmeta.post_id = wp_posts.ID) as "Asking Price (US\$)"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (ZAR)' AND wp_postmeta.post_id = wp_posts.ID) as "Asking Price (ZAR)"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Author' AND wp_postmeta.post_id = wp_posts.ID) as Author
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Binding' AND wp_postmeta.post_id = wp_posts.ID) as Binding
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Book Condition' AND wp_postmeta.post_id = wp_posts.ID) as "Book Condition"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Book Number' AND wp_postmeta.post_id = wp_posts.ID) as "Book Number"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Book Type' AND wp_postmeta.post_id = wp_posts.ID) as "Book Type"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Edition' AND wp_postmeta.post_id = wp_posts.ID) as Edition
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Illustrator' AND wp_postmeta.post_id = wp_posts.ID) as Illustrator
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Inscription' AND wp_postmeta.post_id = wp_posts.ID) as Inscription
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'ISBN' AND wp_postmeta.post_id = wp_posts.ID) as ISBN
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Jacket Condition' AND wp_postmeta.post_id = wp_posts.ID) as "Jacket Condition"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Location' AND wp_postmeta.post_id = wp_posts.ID) as Location
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Published Place' AND wp_postmeta.post_id = wp_posts.ID) as "Published Place"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Published Year' AND wp_postmeta.post_id = wp_posts.ID) as "Published Year"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Publisher' AND wp_postmeta.post_id = wp_posts.ID) as Publisher
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Size' AND wp_postmeta.post_id = wp_posts.ID) as Size
FROM wp_posts
WHERE post_type = 'post'
ORDER BY
post_title
, post_content
Thanks everyone for your input!
Try the many CSV export plugins. I've used this one to great effect.
I think that everything you need is in the *_posts and *_postmeta tables.
There are two ways you can do it. First, just join the *_postmeta table on the *_post table.
"SELECT * FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id"
You will want to enumerate your fields instead of using the asterix, or course. You will end up with multiple rows for each post that you will have to loop through and organize.
Second, write subqueries.
"SELECT *,(SELECT meta_value FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.meta_key = 'Publisher' AND {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID) as Publisher,... FROM {$wpdb->posts}"
Subqueries should perform just fine in this context. Again, enumerate your fields.
Custom Fields Inline SQL Exporting Query
I figured out an intense looking query. It's pulls posts with their custom fields in columns, inline. Note that the 'Concat' in the query is to spoof in some text where the related post would be found and more custom fields can be added at your discretion. My SQL app (Sequel Pro) has a CSV exporting function which I use after querying the remote DB. Work's very well.
SELECT *
FROM (
SELECT
`wp_posts`.`ID` ,
`wp_posts`.`post_title` AS `Title`,
`wp_posts`.`post_date` AS `Date`,
MAX( CASE WHEN `wp_postmeta`.`meta_key` = 'useremail'
THEN `wp_postmeta`.`meta_value`
END ) AS `Email`,
MAX( CASE WHEN `wp_postmeta`.`meta_key` = 'usercell'
THEN `wp_postmeta`.`meta_value`
END ) AS `Cell Phone`,
concat('http://yoururl.com/?p=',`wp_posts`.`ID`) as `URL`
FROM `wp_posts`
LEFT JOIN `wp_postmeta` ON ( `wp_posts`.`ID` = `wp_postmeta`.`post_id` )
WHERE `wp_posts`.`post_status` = 'publish'
AND `wp_posts`.`post_type` = 'customposttype'
GROUP BY `wp_posts`.`ID`
ORDER BY `wp_posts`.`post_date` DESC
) AS `t` WHERE 1 =1
RESULT:

Getting multiple values in multiple rows from multiple tables in MySQL

I'm using WordPress. I want to get everything from the wp_posts table and two values from the postmeta table.
The "wp_postmeta" has the columns "post_id", "meta_key", and "meta_value".
So far, I'm able to get one of the "meta_value":
SELECT wp_post.*, $wpdb->postmeta.meta_value AS votes
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE post_type='post'
AND wp_postmeta.meta_key = 'votes'
However, I also want to get another "meta_value" with the same "post_id" but a different "meta_key". How can I extend this query to the "meta_value"?
If you don't want multiple rows per post, you can do this by having multiple joins:
SELECT wp_post.*, metavotes.meta_value AS votes, metaother.meta_value AS other
FROM wp_posts
INNER JOIN wp_postmeta metavotes
ON ( wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'votes')
INNER JOIN wp_postmeta metaother
ON ( wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'other')
WHERE post_type='post'
(this assumes there is always exactly 1 row for each piece of metadata in wp_postmeta. I'm not familiar enough with wordpress to know whether this is the case. If metadata is optional, use LEFT OUTER JOIN's instead. If you can have multiple, what kind of output do you want?)
How about adding your additional meta_key to the query with the IN clause. Let's say it's the string value foo that you want to add:
SELECT wp_post.*, $wpdb->postmeta.meta_value AS votes
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE post_type='post'
AND wp_postmeta.meta_key IN ('votes', 'foo');
Maybe try something like this:
SELECT wp_posts.*, wp_postmeta.meta_value, wp_postmeta.meta_key
FROM wp_posts INNER JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id)
WHERE wp_posts.post_type='post'
AND wp_postmeta.meta_key IN ('votes', ...)