SQL Subquery (select data from three tables) - mysql

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"}

Related

Subquery returns more than one row in wordpress

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.

delete with 2 join get error - mysql

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)

mySql Distinct - group by issue

I'm trying to find out the most purchased products but to only count distinct users ids. Basically my client wants to stop duplicate purchases from the same user, so that they can't affect the chart/best sellers.
I need to count all order_items for that product, using only Distinct users ids. Currently the results are counting all order_items so the Distinct isn't working.
Any help and I would be grateful.
Thanks in advance
SELECT *
FROM
( SELECT DISTINCT
order_item_meta_3.meta_value as distinct_user_order_items_id,
order_item_meta_2.meta_value as product_id,
SUM( order_item_meta.meta_value ) as item_quantity
FROM
wp_woocommerce_order_items as order_items
LEFT JOIN wp_woocommerce_order_itemmeta as order_item_meta
ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN wp_woocommerce_order_itemmeta as order_item_meta_2
ON order_items.order_item_id = order_item_meta_2.order_item_id
LEFT JOIN wp_woocommerce_order_itemmeta as order_item_meta_3
ON order_items.order_item_id = order_item_meta_3.order_item_id
LEFT JOIN wp_posts AS posts
ON order_items.order_id = posts.ID
LEFT JOIN wp_term_relationships AS rel
ON posts.ID = rel.object_ID
LEFT JOIN wp_term_taxonomy AS tax
USING( term_taxonomy_id )
LEFT JOIN wp_terms AS term
USING( term_id )
WHERE
posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('completed','processing','on-hold')
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_qty'
AND order_item_meta_2.meta_key = '_product_id'
AND order_item_meta_3.meta_key = '_user_id'
GROUP BY
order_item_meta_2.meta_value
ORDER BY
item_quantity DESC ) as order_table,
wp_posts
LEFT JOIN wp_postmeta as mk1
ON wp_posts.ID = mk1.post_id
LEFT JOIN wp_postmeta as mk2
ON wp_posts.ID = mk2.post_id
WHERE
order_table.product_id = wp_posts.ID
AND wp_posts.ID = mk1.post_id
AND mk1.meta_key = 'is_album'
AND mk1.meta_value = 0
AND mk2.meta_key = '_price'
AND mk2.meta_value = 0
Wouldn't you need to do the sum outside the group by. you have to materialize the distinct set first then aggregate it...
so change first few lines to...
SELECT distinct_user_order_items_Id, product_Id, sum(item_Quantity) as item_Quantity
FROM (
SELECT
order_item_meta_3.meta_value as distinct_user_order_items_id,
order_item_meta_2.meta_value as product_id,
order_item_meta.meta_value as item_quantity

SQL Multiple WHERE IN

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

Increase query performance

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