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')
Related
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
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.
I have a table wp_views, with columns postid and views
I want to get the IDs that have the highest values of views (top 4)
Then return the title and link from wp_posts using the postid.
What's the right way of doing this?
You can try the following
global $wpdb;
$top4=$wpdb->get_results('SELECT post_title, post_name from `'.$wpdb->prefix.'views`
INNER JOIN `'.$wpdb->prefix.'posts` ON `postid`=`ID`
ORDER BY `views` DESC
LIMIT 4;', ARRAY_A);
I have tried to replicate your table structure from what your write and from this i have come up with the following:
SELECT id, title, link
FROM wp_views RIGHT JOIN wp_posts ON wp_posts.id = wp_views.post_id
ORDER BY views DESC
LIMIT 4;
you can try it out here: http://sqlfiddle.com/#!9/1cea23/1
I am using RIGHT JOIN to allow null values in the wp_posts part of the result. If you want to avoid NULL values in your results you can use INNER JOIN instead.
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.
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'