MySQL query value + 1 - mysql

I want update column in the table with specific values automatically increasing, i used this query :
Update wp_postmeta
set meta_value = 11622 + 1
WHERE `meta_key` = '_thumbnail_id'
ORDER BY `wp_postmeta`.`post_id` ASC
its work but all values in meta_value column become equal, anyone can help?

You can use the following sql, and "ORDER BY" is not needed in the "update" sql.
Update wp_postmeta
set meta_value = meta_value+1
WHERE `meta_key` = '_thumbnail_id'

Update wp_postmeta
set meta_value = (meta_value + 1)
WHERE  `meta_key` =  '_thumbnail_id'

Related

SQL query? SELECT the inverse of this query?

I am trying to find duplicates of wordpress posts using SQL - but duplicates according to duplicate post meta - not duplicate titles. So far the closest code I could find does the opposite - it finds all the unique posts. How can I reverse this query?
SELECT id,meta_value, post_title, post_content
FROM wp_posts
LEFT JOIN wp_postmeta c ON ( wp_posts.ID = c.post_id )
WHERE post_type = 'post' AND meta_key = 'syndication_permalink'
GROUP BY meta_value
HAVING Count(meta_value) > 1
*UPDATE sorry for being a noob at SQL.. I have added a table to show exactly what the aim is. I want to delete the duplicate posts from freelancer.com
post_id meta_key meta_value
-------- ------------------- ----------------------------------------
1 syndication_permalink https://www.freelancer.com/projects/
2 syndication_permalink https://www.freelancer.com/projects/
3 syndication_permalink https://www.freelancer.com/projects/
4 syndication_permalink https://www.simplyhired.com/job/W6sVJ1
5 syndication_permalink https://www.mandy.com/uk/job/576913/junior
I am not Sure, What you want as your Output. But Try this code and let me know whether it solved your problem or not.
SELECT x.*
FROM wp_posts x
JOIN
(SELECT wp.meta_value
FROM wp_posts wp
LEFT JOIN wp_postmeta c ON ( wp.ID = c.post_id )
WHERE post_type = 'post' AND meta_key = 'syndication_permalink'
GROUP BY wp.meta_value
HAVING Count(wp.meta_value) > 1) as y ON y.meta_value = x.meta_value
This will give all duplicate value as per column meta_value. Let me correct if I am Wrong.

How to set sql request correctly?

I want to get data from column meta_key - 'artikul' and meta_key='_thumbnail_id'.
What I'm doing wrong?
$sql = "SELECT * FROM wp_postmeta
WHERE meta_key='artikul'
AND meta_key='_thumbnail_id'";
The WHERE only works on one row. So, the condition is never true.
I think you want:
SELECT post_id
FROM wp_postmeta
WHERE meta_key IN ('artikul', '_thumbnail_id')
GROUP BY post_id
HAVING COUNT(*) = 2;
This returns posts that have the two keys. This assumes that you don't have duplicate key values on a post. If so, then use:
HAVING COUNT(DISTINCT meta_key) = 2
SELECT * FROM wp_postmeta WHERE (meta_key='artikul') OR (meta_key='_thumbnail_id')

Trying to Multiply Several Values from a table

I have been spending most of the day reading and trying to figure this out. I would like to display a list of post_id when length X width X height => 4000
The meta_value in the table is of type LONGTEXT so it has to be CAST prior to doing any multiplication. I think maybe my SQL order of operations are wrong. It's been a while since I have done this stuff.
SELECT post_id, meta_key
FROM postmeta
WHERE (
DECLARE #temp_length = (SELECT meta_value
FROM postmeta
WHERE meta_key = '_length'),
#temp_width = (SELECT meta_value
FROM postmeta
WHERE meta_key = '_width'),
#temp_height = (SELECT meta_value
FROM postmeta
WHERE meta_key = '_height')
AND
(CAST(#temp_length AS UNSIGNED) * CAST(#temp_width AS UNSIGNED) * CAST(#temp_height AS UNSIGNED) > 250))
ORDER BY post_id;
I am getting ERROR:
MySQL said: Documentation
#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 'DECLARE #temp_length = (SELECT meta_value
FRO' at line 4
Use a query that gets each value into a separate column. Then use HAVING to filter it based on the product.
SELECT post_id,
CAST(MAX(CASE WHEN meta_key = '_length' THEN meta_value END) AS UNSIGNED) AS temp_length,
CAST(MAX(CASE WHEN meta_key = '_width' THEN meta_value END) AS UNSIGNED) AS temp_width,
CAST(MAX(CASE WHEN meta_key = '_height' THEN meta_value END) AS UNSIGNED) AS temp_height
FROM postmeta
GROUP BY post_id
HAVING temp_length * temp_width * temp_height >= 4000

SQL update row based on another row

I have a wordpress database table called "wp_postmeta"
It looks like this (simplified)
Here is what I want to do in english, hopefully someone can help me translate this to an actual SQL statement.
if meta_key(address) LIKE "%Portland%", update meta_value(post_city_id) to be 7,17" where the post_id's are the same.
So in the end it would look like:
I tried this, but no rows selected when executed.
UPDATE wp_postmeta
SET wp_postmeta_bak.meta_value = "7,17"
WHERE wp_postmeta.meta_key = "post_city_id"
AND
wp_postmeta.meta_key = "address" AND wp_postmeta.meta_value LIKE "%Portland"
I read in another questions here about using temporary table, but thats above my pay grade.
Ahhh, the mysterious and wonderful wp_postmeta key/value store sows more mystery and wonder. You need a self-join to correlate the postmeta rows that relate to the same post_id values as each other.
You need to update the right side of a self-joined table.
UPDATE wp_postmeta a
JOIN wp_postmeta b ON a.post_id = b.post_id
AND b.meta_key = 'address'
AND b.meta_value LIKE '%Portland%'
SET a.meta_value = '7,17'
WHERE a.meta_key = 'post_city_id'
The JOIN will yield an empty resultset unless all three of the ON criteria match. Once it yields a nonempty result set, it will update the correct rows.
Before you do this update, you can test that it's choosing the right rows with this query.
SELECT a.meta_id, a.post_id, a.meta_key, a.meta_value,
b.meta_value AS address
FROM wp_postmeta a
JOIN wp_postmeta b ON a.post_id = b.post_id
AND b.meta_key = 'address'
AND b.meta_value LIKE '%Portland%'
WHERE a.meta_key = 'post_city_id'
The rows in post_meta that will be updated have the meta_id values shown in this result set.
You're not getting any results because you're telling the code two different exclusive things:
1) where meta_key = "post_city_id" AND
2) where same column = "address".
From what I can see, it's either one or the other. You can't have both. It's probably a typo.
WHERE wp_postmeta.meta_key = "post_city_id"
AND
wp_postmeta.meta_key = "address"
AND wp_postmeta.meta_value LIKE "%Portland"

MySQL update join with limit

I would like to know how to update a table, with values from another table, by the trick is I need to set a limit because I have thousands of rows to update, and PHPmyadmin can't handle that load. ( I dont have direct access to the server )
My table structure looks like this
wp_postmeta
meta_id,
post_id,
meta_key,
meta_value
wp_map
oldmap, newmap
What I need to do is join the two tables on wp_postmeta.meta_value and wp_map.oldmap, and update the wp_postmeta.meta_value with wp_map.newmap.
Here is my current query, but I need to add a LIMIT of 100 to the query, as I'm splitting the query up into smaller chunks so PHPMyAdmin can process.
UPDATE wp_postmeta
INNER JOIN wp_map
ON wp_map.oldmap = wp_postmeta.meta_value
SET wp_postmeta.meta_value = wp_map.newmap;
I read about creating a subquery, but couldn't find any relevant examples, so if someone could steer me in the right direction or provide a working example it would be greatly appreciated.
You can try it this way
UPDATE wp_postmeta t JOIN
(
SELECT p.meta_id, m.newmap
FROM wp_postmeta p JOIN wp_map m
ON p.meta_value = m.oldmap
ORDER BY p.meta_id
LIMIT 100
) s
ON t.meta_id = s.meta_id
SET t.meta_value = s.newmap;
Here is SQLFiddle demo
sqlFiddle
in this example I am only doing 5 rows, you can change the limit to 0,100 if you'd like
UPDATE wp_postmeta,
(SELECT oldmap,newmap
FROM wp_map
ORDER BY oldmap,newmap limit 0,5)as wp_map
SET wp_postmeta.meta_value = wp_map.newmap
WHERE wp_map.oldmap = wp_postmeta.meta_value
It's rather unconventional , but it should help you in a pinch
SET #tmp_c = 0;
UPDATE
table1
INNER JOIN table2 ON table1.id = table2.id
INNER JOIN table3 ON table2.id = table3.id
SET
table1.remove_date = NOW() - INTERVAL 5 MONTH
WHERE
table1.active = 1
AND
(IF (table1.active = 1, #tmp_c := #tmp_c + 1 , 0 )) //This is to only increment the counter if the whole where matches
AND
#tmp_c <= 15 //Start ignoring row items as soon as it exceeds the limit amount