Trying to Multiply Several Values from a table - mysql

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

Related

Nested MySQL selects issue within WordPress usermeta table

Having an issue with a complex MySql select statement and hoping for some pointers!
So I am using a number of plugins on top of WordPress with some interesting ways of storing data. The bits I'm concerned with are as following: There are some 'parent' accounts which have a number of child accounts. The parent to child relationship is stored in the usermeta table (user_id=user_id of child account, meta_value = parent_id, meta_key='parent'). Each of these child accounts can also complete a number of tasks. This is also stored in the usermeta table (user_id=user_id of child account, meta_value = complete_status, meta_key='task_id_'.task_id).
I'm trying to create a view where I get a list of each of these parent accounts, along with a few bits of information, then a few derived values from their children, including the average number of completed tasks by the children of each parent.
This is my MySQL statement, the part that is having the issue is the nested select:
SELECT
wp_parent_account_info_table.obj_id,
wp_parent_account_info_table.obj_type,
wp_parent_account_info_table.id,
wp_other_custom_table_info.created_at,
wp_other_custom_table_info.product_id,
(SELECT AVG(cc.rcount)
FROM (SELECT DISTINCT COUNT(*) as rcount
FROM wp_usermeta
WHERE meta_key LIKE 'task_id_%'
AND meta_value = 'complete'
AND wp_usermeta.user_id IN (SELECT DISTINCT user_id
FROM wp_usermeta
WHERE meta_key = 'parent'
AND meta_value = wp_parent_account_info_table.id
) AS sc
) AS cc
) AS a
FROM wp_parent_account_info_table
JOIN wp_other_custom_table_info
ON `wp_parent_account_info_table`.`obj_id`=`wp_other_custom_table_info`.`id`
INNER JOIN wp_another_custom_table_info
ON `wp_another_custom_table_info`.`subscription_id`=`wp_parent_account_info_table`.`obj_id` WHERE `wp_parent_account_info_table`.`status` = 'enabled'
AND `wp_parent_account_info_table`.`sub_accounts_available` <> '0'
AND `wp_other_custom_table_info`.`status` = 'active'
AND (`wp_another_custom_table_info`.`expires_at` > CONCAT(CURDATE(), ' 23:59:59')
OR `wp_another_custom_table_info`.`expires_at` = '0000-00-00 00:00:00')
LIMIT 0,30;
I've tried to make this readable, apologies for complexity. I didn't want to remove any parts incase they were relevant.
The statement works fine without the nested select. It also works (has no error) if I replace the most nested select with an array of IDs (so just putting: IN (1,2,3)). Is this something about me trying to get the parent ID from too far down?
This is the error:
#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 'AS sc) AS cc) AS a FROM wp_parent_account_info_table JOIN wp_other_custom_table_info ON'
Any pointers would be much appreciated.
Edit:
Along with the answer below which resolved this error, I also didn't have access to the id variable in the furthest nest (a new error!), so I split them into an extra column. Here is my final code:
SELECT
wp_parent_account_info_table.obj_id,
wp_parent_account_info_table.obj_type,
wp_parent_account_info_table.id,
wp_other_custom_table_info.created_at,
wp_other_custom_table_info.product_id,
(SELECT DISTINCT COUNT(*)
FROM wp_usermeta
WHERE meta_key = 'parent'
AND meta_value = wp_parent_account_info_table.id) as c,
(SELECT DISTINCT COUNT(*) as rcount
FROM wp_usermeta
WHERE meta_key LIKE 'task_id_%'
AND meta_value = 'complete'
AND wp_usermeta.user_id IN (SELECT DISTINCT user_id
FROM wp_usermeta
WHERE meta_key = 'parent'
AND meta_value = wp_parent_account_info_table.id
)
) AS a,
(SELECT a / c)
FROM wp_parent_account_info_table
JOIN wp_other_custom_table_info
ON `wp_parent_account_info_table`.`obj_id`=`wp_other_custom_table_info`.`id`
INNER JOIN wp_another_custom_table_info
ON `wp_another_custom_table_info`.`subscription_id`=`wp_parent_account_info_table`.`obj_id` WHERE `wp_parent_account_info_table`.`status` = 'enabled'
AND `wp_parent_account_info_table`.`sub_accounts_available` <> '0'
AND `wp_other_custom_table_info`.`status` = 'active'
AND (`wp_another_custom_table_info`.`expires_at` > CONCAT(CURDATE(), ' 23:59:59')
OR `wp_another_custom_table_info`.`expires_at` = '0000-00-00 00:00:00')
LIMIT 0,30;
The syntax error is related to your IN cluse ... the IN clause base on a subselect don't require a tablename alias then avoid the sc after the )
SELECT
wp_parent_account_info_table.obj_id,
wp_parent_account_info_table.obj_type,
wp_parent_account_info_table.id,
wp_other_custom_table_info.created_at,
wp_other_custom_table_info.product_id,
(SELECT AVG(cc.rcount)
FROM (SELECT DISTINCT COUNT(*) as rcount
FROM wp_usermeta
WHERE meta_key LIKE 'task_id_%'
AND meta_value = 'complete'
AND wp_usermeta.user_id IN (SELECT DISTINCT user_id
FROM wp_usermeta
WHERE meta_key = 'parent'
AND meta_value = wp_parent_account_info_table.id
)
) AS cc
) AS a
FROM wp_parent_account_info_table
JOIN wp_other_custom_table_info
ON `wp_parent_account_info_table`.`obj_id`=`wp_other_custom_table_info`.`id`
INNER JOIN wp_another_custom_table_info
ON `wp_another_custom_table_info`.`subscription_id`=`wp_parent_account_info_table`.`obj_id` WHERE `wp_parent_account_info_table`.`status` = 'enabled'
AND `wp_parent_account_info_table`.`sub_accounts_available` <> '0'
AND `wp_other_custom_table_info`.`status` = 'active'
AND (`wp_another_custom_table_info`.`expires_at` > CONCAT(CURDATE(), ' 23:59:59')
OR `wp_another_custom_table_info`.`expires_at` = '0000-00-00 00:00:00')
LIMIT 0,30;

Update a row with data from another row in the same table based on a similar field

I am using wordpress and I want to copy data from a custom field to another custom field.
Here is a visually representation of the table:
post_id meta_key meta_value
7 shortcode example text
7 video_url
20 shortcode sample text
20 video_url
I want to copy the meta_value of the shortcode to the meta_value of the video_url if their post_id matches
This is what I have so far but I am getting syntax error:
UPDATE wp_postmeta
SET
meta_value = newdata.meta_value
FROM
(
SELECT
post_id,
meta_value
FROM wp_postmeta
WHERE
meta_key = 'shortcode'
) newdata
WHERE
meta_key = "video_url"
AND
post_id = newdata.post_id
This is the error that I am getting:
#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
'FROM
(
SELECT
post_id,
meta_value
FROM wp_postme' at line 4
You can use join when updating data
UPDATE wp_postmeta old
INNER JOIN wp_postmeta new ON old.post_id = new.post_id
SET old.meta_value = new.meta_value
WHERE old.meta_key = 'video_url' AND new.meta_key = 'shortcode';

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.

Listing meta_value and converting to INT for comparison

I am trying to list all post_id where meta_key = '_length' AND meta_value => 48 however meta_value is LONGTEXT.
I have the first part figured out:
SELECT post_id, meta_key, meta_value
FROM postmeta
WHERE (meta_key = '_length' AND meta_value = '49')
ORDER BY post_id;
I tried using Convert and CAST and keep getting errors. I decided I needed a little help after reading for a few hours.
After more reading I found an answer.
I believe it was just a two part syntax error.
I was trying
SELECT post_id, meta_key, meta_value
FROM postmeta
WHERE (meta_key = '_length' AND (CAST(meta_value AS INT) > 48))
ORDER BY post_id;
However when casting AS I had to switch to UNSIGNED, then I cleaned up the parentheses and the following query works.
SELECT post_id, meta_key, meta_value
FROM postmeta
WHERE (meta_key = '_length' AND CAST(meta_value AS UNSIGNED) > 48)
ORDER BY post_id;

MySQL query value + 1

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'