SQL combine records with same wildcard value - mysql

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;

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%");

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;

How do I select from multiple rows in wp_postmeta table using wpdb select statement Wordpress

I have a select statement like this:
$results = $wpdb->get_results( "SELECT $wpdb->postmeta.meta_value
FROM $wpdb->postmeta
WHERE 1=1 AND $wpdb->postmeta.meta_key = 'geo_short_address'" );
But I'm now trying to filter this statement to only include those 'geo_short_address' that also have an entry or not empty in a meta-key field called '_Company'
They have the same post id, for instance:
post_id meta_key meta_value
53 geo_short_address Nottingham
53 _Company Ledgemonkey
So I only want to return Nottingham if the meta_key _Company exists in the post_id
There are other entries in the DB that will not have the meta_key _Company they are the ones I want to exclude ...?
I have tried various things but can't seem to get the combination..?
To restate your question, you want to filter the postmeta table on multiple criteria. You need to do something like this (edit to include meta_key criteria).
SELECT whatever
FROM $wpdb->postmeta.meta_value
WHERE 1=1
AND $wpdb->postmeta.post_id IN (
SELECT distinct $wpdb->postmeta.post_id
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.post_value = 'Ledgemonkey'
AND $wpdb->postmeta.meta_key = '_Company'
)
AND $wpdb->postmeta.meta_key = 'geo_short_address'
This can be a bit of a performance hairball if you have tens of thousands of postmeta rows, but it will work.
By the way, you can get rid of that ugly but inconsequential 1=1 by judicious use of the implode() function.