SQL query? SELECT the inverse of this query? - mysql

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.

Related

Using Substring_Index with data from a second table

As a follow up to my previous question:
I want to select everything in the wp_posts table that matches:
post_type = "answer"
post_type = "question"
post_type contains revision, preceded by the ID of either one of the previous criteria. For example: 21-revision-v1 or 10903-revision-v1 Where I want to select those posts of which the first numerical part matches the ID of posts selected in the previous 2 requirements.
I now constructed a new table ap_qa which holds all the ID's from posts matching either criteria 1 or 2 above.
Now to select the cases that match criteria 3 I thought of using Substring_Index() as that allows for matches within a string.
My current code is:
SELECT *
FROM `wp_posts` p
WHERE p.post_type IN ('answer', 'question') OR
Substring_Index(p.post_Type,'-revision',1) IN QA.ID
The first rule following where is to satisfy criteria 1 and 2, the last row is meant to satisfy criteria 3. However my syntax is invalid, as is returned.
The error message reads (in Dutch):
#1064 - Er is iets fout in de gebruikte syntax bij 'QA.ID' in regel 4
I now constructed a new table ap_qa which holds all the ID's from posts matching either criteria 1 or 2 above.
You don't at all need a temp table for this. You can get the result that you want directly from the original table in a single query:
select *
from wp_posts wp
where post_type in ('answer', 'question') and exists (
select 1
from wp_posts wp1
where
wp1.post_type in ('answer', 'question')
or wp1.id = substring_index(wp.post_type, '-revision', 1)
)
You need a subquery that returns the ids of the table ap_qa:
SELECT *
FROM `wp_posts` p
WHERE p.post_type IN ('answer', 'question')
OR SUBSTRING_INDEX(p.post_Type,'-revision',1) IN (SELECT ID FROM ap_qa)
Or without the table ap_qa:
SELECT *
FROM `wp_posts` p
WHERE p.post_type IN ('answer', 'question')
OR SUBSTRING_INDEX(p.post_Type,'-revision',1) IN (
SELECT ID FROM `wp_posts`
WHERE p.post_type IN ('answer', 'question')
)
Your systqax is wrong as you error message indicates
Use something like zhis
SELECT *
FROM `wp_posts` p
WHERE p.post_type IN ('answer', 'question')
OR
QA.ID LIKE CONCAT(Substring_Index(p.post_Type,'-revision',1) ,'%')
LIMIT 0, 25

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')

Getting posts from wordpress returns the same result when present in several cathegories

In the home page of a website I need to return the last 3 posts of the blog, created with wordpress.
I have this code:
SELECT p.post_title, p.post_date, p.post_content, wpr.object_id, dt_blog_terms.name, dt_blog_terms.slug
FROM dt_blog_terms
INNER JOIN dt_blog_term_taxonomy ON dt_blog_terms.term_id = dt_blog_term_taxonomy.term_id
INNER JOIN dt_blog_term_relationships wpr ON wpr.term_taxonomy_id = dt_blog_term_taxonomy.term_taxonomy_id
INNER JOIN dt_blog_posts p ON p.ID = wpr.object_id
WHERE taxonomy = 'category'
AND p.post_type = 'post'
AND p.post_status = 'publish'
AND slug != 'notizie-notifiche'
ORDER BY `post_date` DESC
LIMIT 3
As you can see, I have one cathegory (notizie-notifiche) that I want to exclude. This sql string works, but I have a problem when the post has more than one cathegory. In this case, it is returned once for every cathegory, while I want to show it only once in total.
Any idea?
I thought I could use DISTINCT, but it does not seem to work with this kind of SELECT statement.
I can tell you what you need to do, but I can't modify the query to do it. You use the term "category" in your question, but there is no field by that name in your data.
You need to aggregate your data, at the level you want, and then include a having clause. The following group by may solve your problem:
group by p.id
having sum(case when slug = 'notizie-notifiche' then 1 else 0 end) = 0

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.

ORDER BY mysql comma separated values

Hi i am trying to order my database by comma separated value but it doesn't seem to be working correctly for me. here is my mysql query.
SELECT *
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_posts.ID=wp_postmeta.post_id
LEFT JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_id
LEFT JOIN wp_terms ON wp_term_taxonomy.term_id=wp_terms.term_id
WHERE wp_terms.name = 'Dimmers'
AND meta_key = 'feature_number_of_channels'
GROUP BY wp_posts.ID
ORDER BY meta_value ASC
And here is a screenshot.
As you can see from the screenshot they dont seem to be working correctly any help please on how to order these ASC correctly???
Correct order i am looking for is so the highest out of them all then ASC like this
24,48 - 4,6,12,24 - 12 - 6 - 6 - 3
Change your ORDER BY to look at the integer value of the string :
ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC
Here's an example:
SELECT "24,38" AS meta_value UNION
SELECT 1 AS meta_value UNION
SELECT 30 AS meta_value
ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC
returns:
Rows = 3
meta_value
24,38
30
3
Good luck!
Using order table.field ASC might help you as there are too many tables joined. That way the server would take the exact order from one single table.