I am trying to select the ID from a wp_posts table where I need to do 3 conditions.
Is the ID matching with the post_id from the wp_postmeta table where the meta_key = 'gtp_analytics_client_id' and the meta_value is not empty.
Is the ID matching with the post_id from the wp_postmeta table where the meta_key = 'gtp_conversion_uploaded' and the meta_value is not equal to 1.
Is the ID matching with the post_id from the wp_postmeta table where the meta_key = 'gtp_lead_revenue' and the meta_value is not empty.
I am a beginner with SQL. This is what I have now, but I cannot use multiple IN's. So I think I need to do it another way.
SELECT ID
FROM wp_posts
WHERE ID IN (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'gtp_analytics_client_id' AND meta_value != '')
AND IN (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'gtp_conversion_uploaded' AND meta_value != 1)
AND IN (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'gtp_revenue' AND meta_value != '')
I get the following error:
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 'IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'gtp_conversion_uploaded' A' at line 4
The and is not part of the in operator, it is three separate in operators, so you need the first operand (ID) for all of them:
SELECT ID
FROM wp_posts
WHERE ID IN ( ... )
AND ID IN ( ... )
AND ID IN ( ... )
You could also write that as three joins:
SELECT
p.ID
FROM
wp_posts p
INNER JOIN wp_postmeta m1 ON m1.post_id = p.ID AND m1.meta_key = 'gtp_analytics_client_id' AND m1.meta_value != ''
INNER JOIN wp_postmeta m2 ON m2.post_id = p.ID AND m2.meta_key = 'gtp_conversion_uploaded' AND m2.meta_value != 1
INNER JOIN wp_postmeta m3 ON m3.post_id = p.ID AND m3.meta_key = 'gtp_revenue' AND m3.meta_value != ''
When it can be either of the 3 cases
SELECT ID
FROM wp_posts
WHERE ID IN (SELECT post_id
FROM wp_postmeta
WHERE (meta_key = 'gtp_analytics_client_id' AND meta_value != '')
OR (meta_key = 'gtp_conversion_uploaded' AND meta_value != 1)
OR (meta_key = 'gtp_revenue' AND meta_value != '')
)
SELECT p.ID
FROM wp_posts p
JOIN wp_postmeta m on p.id = m.post_id
group by p.id
having sum(m.meta_key = 'gtp_analytics_client_id' AND m.meta_value != '') > 0
and sum(m.meta_key = 'gtp_conversion_uploaded' AND m.meta_value != 1) > 0
and sum(m.meta_key = 'gtp_revenue' AND m.meta_value != '') > 0
Related
The part of the code where I write,
(SELECT meta_value FROM wp_postmeta WHERE meta_key = '_regular_price') price
It gives an error that subquery returns more than one row. How to get the price?
SELECT p1.ID,p1.post_title,p1.post_excerpt,p2.meta_value, p1.guid,
(select p.guid from wp_posts as p where p2.meta_value=p.ID and post_type='attachment') img, (SELECT meta_value FROM wp_postmeta WHERE meta_key = '_regular_price') price
FROM wp_posts p1
JOIN wp_postmeta p2
ON p1.ID = p2.post_id AND
p1.post_type = 'product' AND
p1.post_status = 'publish' AND
p2.meta_key = '_thumbnail_id'
JOIN wp_posts p3
ON p3.ID = p2.post_id
add CONCAT() in your subquery to group all your result with comma separate & then use FIND_IN_SET for the main query.
I'm trying to run this delete query in MYSQL (via phpMyAdmin) ANd I keep getting this error :
DELETE
p,pm
from wp_posts p
inner join wp_postmeta pm on pm.post_id = p.id
where p.id in (SELECT MIN( id ) AS min_id
FROM wp_posts inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id and meta_key = 'old_id')
WHERE post_type = 'post'
GROUP BY meta_value
HAVING COUNT( * ) > 1)
any idea why ?
Looking to your code
You don't should use as in subselect for IN clause
DELETE p, pm
from wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.id
where p.id in (SELECT MIN( id )
FROM wp_posts
inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id
and meta_key = 'old_id')
WHERE post_type = 'post'
GROUP BY meta_value
HAVING COUNT( * ) > 1)
I'm using this query to get data from mysql. Can it be improved? It takes around 0.2s for each query...
I found some other way such as left join but I can't get all data with one query.
Or do I need to use two queries?
Select p.id, p.post_date, p.post_content, p.post_title, p.post_name, p.guid
for loop -> likes_count, dislikes_count, post_views_count, rate where post_id = p.id?
SELECT p.id, p.post_date, p.post_content, p.post_title, p.post_name, p.guid,
(SELECT guid FROM wp_posts WHERE id = (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "_thumbnail_id")) AS thumbnail_url,
(SELECT GROUP_CONCAT(DISTINCT term_taxonomy_id) FROM wp_term_relationships WHERE object_id = p.id) AS term_taxonomy_id,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "likes_count" LIMIT 1) AS likes_count,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "dislikes_count" LIMIT 1) AS dislikes_count,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "post_views_count" LIMIT 1) AS post_views_count,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "rate" LIMIT 1) AS rate
FROM wp_posts AS p INNER JOIN wp_term_relationships AS t ON p.id = t.object_id
WHERE p.post_status="publish"
AND p.ping_status="open"
AND t.term_taxonomy_id = ? GROUP BY p.id LIMIT ?,?
[EDIT-1]
[Table structure]1
[EDIT-2]
The Output
{"id":11111,"post_date":"2016-01-02T05:46:58.000Z","post_content":"contenttttttt","post_title":"titleeeee","post_name":"postname","guid":"1255322","thumbnail_url":"thumb1957.jpg","term_taxonomy_id":"7","likes_count":"1","dislikes_count":"1","post_views_count":"538","rate":"50"}
with the following SQL query I check whether a meta value of a (Wordress) post is not equal to 1. But I also want to check if the meta_key 'gtp_conversion_uploaded' exists. And if it not exists I still want to select the ID.
SELECT DISTINCT p.ID
FROM wp_posts p
LEFT JOIN wp_postmeta m2 ON m2.post_id = p.ID
AND m2.meta_key = 'gtp_conversion_uploaded'
AND m2.meta_value != 1
So my query needs to do the following checks:
Select the results where meta_key 'gtp_conversion_uploaded' is not
equal to 1.
And also if the meta_key 'gtp_conversion_uploaded' not exists.
I tried it with a LEFT JOIN, but the problem is that if the meta_value of meta_key 'gtp_conversion_uploaded' is equal to 1, I still get results.
SELECT DISTINCT p.ID
FROM wp_posts p
LEFT JOIN wp_postmeta m2 ON m2.post_id = p.ID
AND m2.meta_key = 'gtp_conversion_uploaded'
WHERE m2.meta_key is null
OR m2.meta_value != 1
I'm trying to optimize this query:
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'passenger_group_id'
AND meta_value in (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'group_event' AND meta_value = '14608')
AND post_id IN (SELECT post_id
FROM wp_postmeta
WHERE meta_value='Cancelled')
AND post_id NOT IN (SELECT ID
FROM wp_posts
WHERE post_status='trash')
Any help would be greatly appreciated.
you can use EXISTS clause alone and add NOT IN condition can be made NOT EXISTS
select wp1.post_id
from wp_postmeta wp1
WHERE meta_key = 'passenger_group_id'
and exists ( select 1 FROM
wp_postmeta wp2
where ( (wp2.meta_key = 'group_event' AND wp2.meta_value = '14608' ) or wp2.meta_value ='Cancelled' )
and wp2.post_id = wp1.meta_value
)
and not exists ( select 1 from
wp_posts p
on p.id = wp1.post_id
and p.post_status = 'trash')
)