Listing meta_value and converting to INT for comparison - mysql

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;

Related

exclude a meta_key from replace query for all meta_value

I have to change the word $15 in all meta_value except 1 meta_key name : pricing_tier
UPDATE postmeta SET meta_value = REPLACE(meta_value, '$15', '$10');
I couldn't find anyway to exclude pricing_tier ACF field from the query.
any help will be appreciated.

How to include additional conditions inside a main WHERE clause? (like an if inside a higher-level if conditional)?

I have a table aus_woocommerce_order_itemmeta with the following schema:
How can I create a query that yields the following logic?
SELECT * FROM aus_woocommerce_order_itemmeta WHERE order_item_id = 660
/* And only from those items, the ones with order_item_id = 660
SELECT the rows that meet any of the following conditions: */
meta_key LIKE "%bebida caliente%"
meta_key LIKE "%elige%"
meta_key LIKE "%agregar%";
This is my current query:
SELECT order_item_id, meta_key, meta_value
FROM aus_woocommerce_order_itemmeta WHERE order_item_id = 660
AND meta_key LIKE "%bebida caliente%"
OR meta_key LIKE "%elige%"
OR meta_key LIKE "%agregar%";
But obviously, the OR statements are on the same level as the first WHERE condition, so it's returning everything, regardless of its order_item_id.
Just wrap the or statement in a parenthesis like this.
SELECT order_item_id, meta_key, meta_value
FROM aus_woocommerce_order_itemmeta WHERE order_item_id = 660
AND (meta_key LIKE "%bebida caliente%"
OR meta_key LIKE "%elige%"
OR meta_key LIKE "%agregar%");

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 combine records with same wildcard value

I have a MySQL Wordpress database and I'm dealing with the postmeta table which isn't nicely structure. It has four columns: "meta_id, post_id, meta_key, meta_value". This is the query I am working with:
SELECT DISTINCT *
FROM wp_10_postmeta as pm
JOIN wp_10_posts AS p ON pm.post_id = p.ID
WHERE (meta_key LIKE 'personnel_%_last_name' OR
meta_key LIKE 'personnel_%_first_name' OR
meta_key LIKE 'personnel_%_title' OR
meta_key LIKE 'personnel_%_alias' OR
meta_key LIKE 'webpages_%_title' OR
meta_key LIKE 'webpages_%_alias' OR
meta_key LIKE 'departments_%_name' OR
meta_key LIKE 'departments_%_alias')
AND post_status = 'publish' AND meta_value LIKE '%john%' AND meta_value LIKE '%smith%'
Issue: This query works great if I search for "john" or "Smith" but if I do "John Smith" it return empty. Is there a way to combine "personnel_%_last_name" and "personnel_%_first_name" and then compare?
I am guessing that you want to look across multiple fields for the match. Try using a group by for this purpose:
SELECT p.*
FROM wp_10_postmeta as pm JOIN
wp_10_posts AS p
ON pm.post_id = p.ID
WHERE (meta_key LIKE 'personnel_%_last_name' OR
meta_key LIKE 'personnel_%_first_name' OR
meta_key LIKE 'personnel_%_title' OR
meta_key LIKE 'personnel_%_alias' OR
meta_key LIKE 'webpages_%_title' OR
meta_key LIKE 'webpages_%_alias' OR
meta_key LIKE 'departments_%_name' OR
meta_key LIKE 'departments_%_alias')
AND p.post_status = 'publish'
GROUP BY p.id
HAVING SUM(meta_value LIKE '%john%') > 0 AND SUM(meta_value LIKE '%smith%') > 0;

How to add a custom field with a random value to all the post

I'm trying to add/update a custom field values for all the post for my wordpress site with the code below ... in phpmysql
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'views'
AS meta_key 'RAND(6)*20' AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'views')
`` AND post_type = 'post';
But this is not working at all ....
Can somebody help me with this !
Instead of 'RAND(6)*20' AS meta_value, use RAND(6)*20 AS meta_value
Basically remove single quotes.