How do I optimize this query? - mysql

SELECT DISTINCT wposts.*
FROM wp_2_posts wposts, wp_2_postmeta wpostmeta, wp_2_postmeta wpostmeta1, wp_2_term_taxonomy, wp_2_terms, wp_2_term_relationships
WHERE wposts.ID = wpostmeta.post_id
AND wp_2_terms.term_id = '8'
AND wp_2_term_taxonomy.term_id = wp_2_terms.term_id
AND wp_2_term_taxonomy.term_taxonomy_id = wp_2_term_relationships.term_taxonomy_id
AND wp_2_term_relationships.object_id = wposts.ID
AND wpostmeta.meta_key = 'validity'
AND wpostmeta.meta_value > '".$logic_date."'
AND wpostmeta1.meta_key != 'permanent'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC
Good advice so far, heres the 'evolved' query which still does not use the second meta_key != 'permanent_listing'
SELECT wposts.*
FROM wp_2_posts wposts LEFT JOIN wp_2_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_2_term_relationships wrelationships ON wrelationships.object_id = wposts.ID
LEFT JOIN wp_2_term_taxonomy wtaxonomy ON wtaxonomy.term_taxonomy_id = wrelationships.term_taxonomy_id
LEFT JOIN wp_2_terms wterms ON wtaxonomy.term_id = wterms.term_id
WHERE
wterms.term_id = '--category id here--' AND
wpostmeta.meta_key = 'wpx_validity' AND
wpostmeta.meta_value > '--todays date here--' AND
wpostmeta.meta_key != 'permanent_listing' AND
wposts.post_status = 'publish' AND
wposts.post_type = 'post'
ORDER BY wposts.post_date DESC
LEFT JOIN and INNER JOIN seem to do the same thing.
the following indexes exist: wp_2_terms.term_id, wp_term_taxonomy.term_taxonomy_id, wp_2_term_relationships.object_id
wp_2_postmeta has a meta_id index, and fields: post_id, meta_key, meta_value.
this query is running on WPMU.

At first I recommend that you use the INNER JOIN syntax to join the tables:
SELECT table1.c1, table2.c2 FROM table1
INNER JOIN table2 ON table1.ck = table2.ck
Also you should avoid using the * operator and carefully select the columns you need. DISTINCT also slows down your query. Do you really need it?

First, your wpostmeta1 does not join with anything.
Second, you don't need DISTINCT here, you need IN.
Try this:
SELECT wposts.*
FROM wp_2_posts wposts
JOIN wp_2_postmeta wpostmeta
ON wpostmeta.post_id = wposts.ID
AND wpostmeta.meta_key = 'validity'
AND wpostmeta.meta_value > '".$logic_date."'
JOIN wp_2_postmeta wpostmeta1,
ON wpostmeta1.post_id = wposts.ID
AND wpostmeta1.meta_key != 'permanent'
WHERE wposts.ID IN
(
SELECT wp_2_term_relationships.object_id
FROM wp_2_term_relationships
JOIN wp_2_term_taxonomy
ON wp_2_term_taxonomy.term_taxonomy_id = wp_2_term_relationships.term_taxonomy_id
WHERE wp_2_term_taxonomy.term_id = '8'
)
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BYc
wposts.post_date DESC
Create the following indexes (or make sure they exist):
wp_2_term_relationships (object_id, term_taxonomy_id)
wp_2_term_taxonomy (term_taxonomy_id, term_id)
wp_2_posts (post_status, post_type, post_date, id)

if the query is too slow,
there are different solutions.
First of all try to create some indexes,
then try to reduce the joins filtering the some of the data with php.
if it is still slow, probably the Database schema is wrong and should be changed.

I'll bet you $5 it is an indexing problem. The first thing I'd do is make sure all the fields you are joining the tables on are indexed.

Related

Wordpress - MySQL query to show posts only from a specific category

I have the following query that bring the posts that contain a specific word in the title
SELECT posts_post.ID AS post_ID,
posts_post.post_date AS post_post_date,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post
FROM wp_posts AS posts_post
WHERE 1=1
AND posts_post.post_title LIKE '%HOTARAR%'
AND posts_post.post_type = 'post'
GROUP BY post_post_date
The problem now is that I need to bring the posts only from a specific category (tag slug for the category is hotarari-consiliu-local and has the ID 160), how could I modify the above query to bring posts only from a single blog posts category? Thanks!
Try the below query.
global $wpdb;
$make = $wpdb->get_results("
SELECT * FROM
$wpdb->posts
LEFT JOIN
$wpdb->term_relationships
ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN
$wpdb->term_taxonomy
ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.post_type = 'post'
AND
$wpdb->posts.post_title LIKE '%HOTARAR%'
AND
$wpdb->term_taxonomy.taxonomy = 'category'
AND
$wpdb->term_taxonomy.term_id = 160
ORDER BY
post_date DESC
");
Assuming Bhautik is on the right path, then something like this should work (I don't know where 'hotarari-consiliu-local' belongs in this).
SELECT p.ID post_ID
, p.post_date post_post_date
, CONCAT('',p.post_title,'') post_title_with_link_to_post
FROM wp_posts p
JOIN wp_term_relationships pt
ON pt.object_id = p.ID
JOIN wp_term_taxonomy t
ON t.term_taxonomy_id = pt.term_taxonomy_id
WHERE p.post_type = 'post'
AND p.post_title LIKE '%HOTARAR%'
AND t.taxonomy = 'category'
AND t.term_id = 160
ORDER
BY post_date DESC

Difficulty with SQL and WPDB in particular in the WHERE clause

I'm Stumped
I have 1/2 a dozen queries that all return expected results in MySQL Workbench, however, return nothing when called from WPDB.
I have tested WPDB works with very simple select statements, however, when they get more difficult WPDB doesn't like what I'm feeding it. I have tried for 2 days before turning here to ask - sadly I get no errors from WPDB just no results.
The query below returns me a stock date that any future orders will be able to ship. If run through myself. But return empty arrays in WPDB.
I understand this SQL needs some work, needs to be run through prepare and should be using the WPDB prefix - All this tidy up will come if I can get it working.
I have found that by simply leaving off the where clause I get a result - so this is where I have been focussing. CAN ANYONE SEE WHAT I HAVE DONE WRONG? or MOST WRONG?
Select stocketa as NextDispatch From
(
Select ETA as stocketa,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH FROM
(
Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH
FROM wp_posts p
LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date')
LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id)
LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' )
LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' )
LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock')
Where OIM2.meta_value = 5734 and p.post_status='atum_pending'
Order By ETA ASC
) as q1
) as q2
WHERE q2.QtyInbound_rt > q2.InverseSOH
LIMIT 1
This is how I am attempting to run it with WPDB (My first attempt at use of WP and WPDB so ... please dont laugh
$ProdID = get_the_ID();
echo ("Prod ID:" . $ProdID);
if(!defined('DIEONDBERROR')) define( 'DIEONDBERROR', true );
global $wpdb;
$selectString ="
select ETA as inStockETA, QtyInbound_rt as QI,InverseSOH as ISOI FROM ( Select ETA,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH
FROM ( Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH FROM wp_posts p
LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date') LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id)
LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' )
LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' )
LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock')
Where OIM2.meta_value = 5734 and p.post_status='atum_pending' Order By ETA ASC
) as q1
) as q2
WHERE q2.QtyInbound_rt > q2.InverseSOH LIMIT 1
";
//$selectString = $wpdb->prepare($selectString,$ProdID);
echo("Select String:" . $selectString);
//$nextShip = $wpdb->get_results($selectString);
//$nextShip = $wpdb->get_results($selectString,ARRAY_A);
$wpdb->show_errors();
$nextShip = $wpdb->get_results($selectString);
$wpdb->print_error();
$wpdb->hide_errors();
After putting in DIEONDBERROR ()
The output is :
Prod ID:5734Select String: select ETA as inStockETA, QtyInbound_rt as QI,InverseSOH as ISOI FROM ( Select ETA,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH FROM ( Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date') LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id) LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' ) LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' ) LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock') Where OIM2.meta_value = 5734 and p.post_status='atum_pending' Order By ETA ASC ) as q1 ) as q2 WHERE q2.QtyInbound_rt > q2.InverseSOH LIMIT 1
WordPress database error: [] select ETA as inStockETA, QtyInbound_rt as QI,InverseSOH as ISOI FROM ( Select ETA,(#runtot := #runtot + q1.QtyInbound) AS QtyInbound_rt, (q1.SOH * -1) as InverseSOH FROM ( Select pm1.meta_value AS ETA, OIM1.meta_value AS QtyInbound, pm2.meta_value as SOH FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID AND pm1.meta_key = '_expected_at_location_date') LEFT JOIN wp_atum_order_items OI ON ( OI.order_id = p.id) LEFT JOIN wp_atum_order_itemmeta OIM1 ON (OIM1.order_item_id =OI.order_item_id AND OIM1.meta_key='_qty' ) LEFT JOIN wp_atum_order_itemmeta OIM2 ON (OIM2.order_item_id =OI.order_item_id AND OIM2.meta_key='_product_id' ) LEFT JOIN wp_postmeta pm2 ON ( pm2.post_id = OIM2.meta_value and pm2.meta_key = '_stock') Where OIM2.meta_value = 5734 and p.post_status='atum_pending' Order By ETA ASC ) as q1 ) as q2 WHERE q2.QtyInbound_rt > q2.InverseSOH LIMIT 1
To know what is happening exactly with your query, use the following piece of code:
$wpdb->show_errors();
$nextShip = $wpdb->get_results($selectString);
$wpdb->print_error();
$wpdb->hide_errors();
and let me know the generated output.
By the way, there are some syntax errors in your query:
You have entered JOI$ but the correct would be JOIN
There is an extra LEFT JOIN before the WHERE clause that should not be there.
Please, check the query you are assigning to the $selectString variable.

Combine query that relies on resultset of another

I run this query to get 20 random items from my wordpress database based on things like rating, category, etc
SELECT (A.user_votes/A.user_voters) as site_rating, B.ID as post_id, B.post_author, B.post_date,E.name as category
FROM `wp_gdsr_data_article` as A
INNER JOIN `wp_posts` as B ON (A.post_id = B.id)
INNER JOIN wp_term_relationships C ON (B.ID = C.object_id)
INNER JOIN wp_term_taxonomy D ON (C.term_taxonomy_id = D.term_taxonomy_id)
INNER JOIN wp_terms E ON (D.term_id = E.term_id)
WHERE
B.post_type = 'post' AND
B.post_status = 'publish' AND
D.taxonomy='category' AND
E.name NOT IN ('Satire', 'Declined', 'Outfits','Unorganized', 'AP')
ORDER BY RAND()
LIMIT 20
Then, for each result of the random items, I want to find a corresponding item that is very similar to the random item (around the same rating) but not identical and also one the user has not seen:
SELECT ABS($site_rating-(A.user_votes/A.user_voters)) as diff, (A.user_votes/A.user_voters) as site_rating, B.ID as post_id, B.post_author, B.post_date,E.name as category ,IFNULL(F.count,0) as count
FROM `wp_gdsr_data_article` as A
INNER JOIN `wp_posts` as B ON (A.post_id = B.id)
INNER JOIN wp_term_relationships C ON (B.ID = C.object_id)
INNER JOIN wp_term_taxonomy D ON (C.term_taxonomy_id = D.term_taxonomy_id)
INNER JOIN wp_terms E ON (D.term_id = E.term_id)
LEFT JOIN (
SELECT *,COUNT(*) as count FROM `verus` WHERE ip = '{$_SERVER['REMOTE_ADDR']}'
) as F ON (A.post_id = F.post_id_winner OR A.post_id = F.post_id_loser)
WHERE
E.name = '$category' AND
B.ID <> '$post_id' AND
B.post_type = 'post' AND
B.post_status = 'publish' AND
D.taxonomy='category' AND
E.name NOT IN ('Satire', 'Declined', 'Outfits','Unorganized', 'AP')
ORDER BY count ASC, diff ASC
LIMIT 1
Where the following php variables refer to the result of the previous query
$post_id = $result['post_id'];
$category = $result['category'];
$site_rating = $result['site_rating'];
and $_SERVER['REMOTE_ADDR'] refers to the user's IP.
Is there a way to combine the first query with the 20 additional queries that need to be called to find corresponding items, so that I need just 1 or 2 queries?
Edit: Here is the view that simplifies the joins
CREATE VIEW `versus_random` AS
SELECT (A.user_votes/A.user_voters) as site_rating, B.ID as post_id, B.post_author, B.post_date,E.name as category
FROM `wp_gdsr_data_article` as A
INNER JOIN `wp_posts` as B ON (A.post_id = B.id)
INNER JOIN wp_term_relationships C ON (B.ID = C.object_id)
INNER JOIN wp_term_taxonomy D ON (C.term_taxonomy_id = D.term_taxonomy_id)
INNER JOIN wp_terms E ON (D.term_id = E.term_id)
WHERE
B.post_type = 'post' AND
B.post_status = 'publish' AND
D.taxonomy='category' AND
E.name NOT IN ('Satire', 'Declined', 'Outfits','Unorganized', 'AP')
My attempt now with the view:
SELECT post_id,
(
SELECT INNER_TABLE.post_id
FROM `versus_random` as INNER_TABLE
WHERE
INNER_TABLE.post_id <> OUTER_TABLE.post_id
ORDER BY (SELECT COUNT(*) FROM `versus` WHERE ip = '54' AND (INNER_TABLE.post_id = post_id_winner OR INNER_TABLE.post_id = post_id_loser)) ASC
LIMIT 1
) as innerquery
FROM `versus_random` as OUTER_TABLE
ORDER BY RAND()
LIMIT 20
However the query just timesout and freezes my mysql.
I think it should work like this, but I don't have any Wordpress at hand to test it. The second query that gets the related post is embedded in the other query, when it gets just the related_post_id. The whole query is turned into a subquery itself, given the alias 'X' (although you are free to use 'G', if you want to continue your alphabet.)
In the outer query, the tables for posts and data-article are joined again (RA and RP) to query the relevant fields of the related post, based on the related_post_id from the inner query. These two tables are left joined (and in reverse order), so you still get the main post if no related post was found.
SELECT
X.site_rating,
X.post_id,
X.post_author,
X.post_date,
X.category,
RA.user_votes / RA.user_voters as related_post_site_rating,
RP.ID as related_post_id,
RP.post_author as related_post_author,
RP.post_date as related_post_date,
RP.name as related_category,
FROM
( SELECT
(A.user_votes/A.user_voters) as site_rating,
B.ID as post_id, B.post_author, B.post_date,E.name as category,
( SELECT
RB.ID as post_id
FROM `wp_gdsr_data_article` as RA
INNER JOIN `wp_posts` as RB ON (RA.post_id = RB.id)
INNER JOIN wp_term_relationships RC ON (RB.ID = RC.object_id)
INNER JOIN wp_term_taxonomy RD ON (RC.term_taxonomy_id = RD.term_taxonomy_id)
INNER JOIN wp_terms RE ON (RD.term_id = RE.term_id)
LEFT JOIN (
SELECT *,COUNT(*) as count FROM `verus` WHERE ip = '{$_SERVER['REMOTE_ADDR']}'
) as RF ON (RA.post_id = RF.post_id_winner OR RA.post_id = RF.post_id_loser)
WHERE
RE.name = E.name AND
RB.ID <> B.ID AND
RB.post_type = 'post' AND
RB.post_status = 'publish' AND
RD.taxonomy='category' AND
RE.name NOT IN ('Satire', 'Declined', 'Outfits','Unorganized', 'AP')
ORDER BY count ASC, diff ASC
LIMIT 1) as related_post_id
FROM `wp_gdsr_data_article` as A
INNER JOIN `wp_posts` as B ON (A.post_id = B.id)
INNER JOIN wp_term_relationships C ON (B.ID = C.object_id)
INNER JOIN wp_term_taxonomy D ON (C.term_taxonomy_id = D.term_taxonomy_id)
INNER JOIN wp_terms E ON (D.term_id = E.term_id)
WHERE
B.post_type = 'post' AND
B.post_status = 'publish' AND
D.taxonomy='category' AND
E.name NOT IN ('Satire', 'Declined', 'Outfits','Unorganized', 'AP')
ORDER BY RAND()
LIMIT 20
) X
LEFT JOIN `wp_posts` as RP ON RP.id = X.related_post_id
LEFT JOIN `wp_gdsr_data_article` as RA.post_id = RP.id
I can't test my proposal so take it with the benefit of the doubt. Anyway i hope it could be a valid starting point for some of the issues faced.
I can not imagine a solution that does not pass through a temporary table, cabling onerous computations present in your queries. You could also have the goal to not interfere with the randomization of the first phase. In the following I try to clarify.
I'll start with these rewritings:
-- first query
SELECT site_rating, post_id, post_author, post_date, category
FROM POSTS_COMMON
ORDER BY RAND()
LIMIT 20
-- second query
SELECT ABS(R.site_rating_A - R.site_rating_B) as diff, R.site_rating_B as site_rating, P.post_id, P.post_author, P.post_date, P.category, F.count
FROM POSTS_COMMON AS P
INNER JOIN POSTS_RATING_DIFFS AS R ON (P.post_id = R.post_id_B)
LEFT JOIN (
/* post_id_winner, post_id_loser explicited; COUNT(*) NULL treatment anticipated */
SELECT post_id_winner, post_id_loser, IFNULL(COUNT(*), 0) as count FROM `verus` WHERE ip = '{$_SERVER['REMOTE_ADDR']}'
) as F ON (P.post_id = F.post_id_winner OR P.post_id = F.post_id_loser)
WHERE
P.category = '$category'
AND R.post_id_A = '$post_id'
ORDER BY count ASC, diff ASC
LIMIT 1
with:
SELECT A.post_id_A, B.post_id_B, A.site_rating as site_rating_A, B.site_rating as site_rating_B
INTO POSTS_RATING_DIFFS
FROM POSTS_COMMON as A, POSTS_COMMON as B
WHERE A.post_id <> B.post_id AND A.category = B.category
CREATE VIEW POSTS_COMMON AS
SELECT A.ID as post_id, A.user_votes, A.user_voters, (A.user_votes / A.user_voters) as site_rating, B.post_author, B.post_date, E.name as category
FROM wp_gdsr_data_article` as A
INNER JOIN `wp_posts` as B ON (A.post_id = B.post_id)
INNER JOIN wp_term_relationships C ON (B.ID = C.object_id)
INNER JOIN wp_term_taxonomy D ON (C.term_taxonomy_id = D.term_taxonomy_id)
INNER JOIN wp_terms E ON (D.term_id = E.term_id)
WHERE
B.post_type = 'post' AND
B.post_status = 'publish' AND
D.taxonomy='category' AND
E.name NOT IN ('Satire', 'Declined', 'Outfits','Unorganized', 'AP')
POSTS_COMMON isolates a common view between the two queries.
With POSTS_RATING_DIFFS, a temporary table populated with the ratings combinations and diffs, we have "the trick" of transforming the inequality join criteria on post_id(s) in an equality one (see R.post_id_A = '$post_id' in the second query).
We also take advantage of a temporary table in having precomputed ratings for the combinatory explosion of A.post_id <> B.post_id (with post category equality), and moreover being useful for other sessions.
Also extracting the RAND() ordering in a temporary table could be advantageous. In this case we could limit the ratings combinations and diffs only on the 20 randomly chosen.
Original limiting to one single row in the dependent second level query is done by mean of ordering and limit statements.
The proposed solution avoids elaborating a LIMIT 1 on an ORDER BY resultset in the second level query wich become a subquery.
The single row calculation in the subquery is done by mean of a WHERE criteria on the maximum of a single value calculated from the columns values on which ORDER BY clause is used.
The combination into a single value must be valid in preserving the correct ordering. I'll leave in pseudo-code as:
'<combination of count and diff>'
For example, using combination of the two values into a string type, we could have:
CONCAT(LPAD(CAST(count AS CHAR), 10, '0'), LPAD(CAST(ABS(diff) AS CHAR), 20, '0'))
The structure of the single query would be:
SELECT (Q_LVL_1.user_votes/Q_LVL_1.user_voters) as site_rating_LVL_1, Q_LVL_1.post_id as post_id_LVL_1
, Q_LVL_1.post_author as post_author_LVL_1, Q_LVL_1.post_date as post_date_LVL_1
, Q_LVL_1.category as category_LVL_1, Q_LVL_2.post_id as post_id_LVL_2
, Q_LVL_2.diff as diff_LVL_2, Q_LVL_2.site_rating as site_rating_LVL_2
, Q_LVL_2.post_author as post_author_LVL_2, Q_LVL_2.post_date as post_date_LVL_2
, Q_LVL_2.count
FROM POSTS_COMMON AS Q_LVL_1
, /* 1-row-selection query placed side by side for each Q_LVL_1's row */
(
SELECT CORE_P.post_id, CORE_P.ABS_diff as diff, P.site_rating, P.post_author, P.post_date, CORE_P.count
FROM POSTS_COMMON AS P
INNER JOIN (
SELECT FIRST(CORE_P.post_id) as post_id, ABS(CORE_P.diff) as ABS_diff, CORE_P.count
FROM (
/*
selection of posts with post_id(s) different from first level query,
not already taken and with the topmost value of
'<combination of count and diff>'
*/
) AS CORE_P
GROUP BY CORE_P.count, ABS(CORE_P.diff)
/* the one row selector */
) AS CORE_ONE_LINER ON P.post_id = CORE_ONE_LINER.post_id
) AS Q_LVL_2
ORDER BY RAND()
LIMIT 20
CORE_P selection could have more post_id(s) corresponding to the topmost value '<combination of count and diff>', so the use of GROUP BY and FIRST clauses to reach the single row.
This brings to a possible final implementation:
SELECT (Q_LVL_1.user_votes/Q_LVL_1.user_voters) as site_rating_LVL_1, Q_LVL_1.post_id as post_id_LVL_1
, Q_LVL_1.post_author as post_author_LVL_1, Q_LVL_1.post_date as post_date_LVL_1
, Q_LVL_1.category as category_LVL_1, Q_LVL_2.post_id as post_id_LVL_2
, Q_LVL_2.diff as diff_LVL_2, Q_LVL_2.site_rating as site_rating_LVL_2
, Q_LVL_2.post_author as post_author_LVL_2, Q_LVL_2.post_date as post_date_LVL_2
, Q_LVL_2.count
FROM POSTS_COMMON AS Q_LVL_1
, (
SELECT CORE_P.post_id, CORE_P.ABS_diff as diff, P.site_rating, P.post_author, P.post_date, CORE_P.count
FROM POSTS_COMMON AS P
INNER JOIN
(
SELECT FIRST(CORE_P.post_id) as post_id, ABS(CORE_P.diff) as ABS_diff, CORE_F.count
FROM (
SELECT CORE_RATING.post_id as post_id, ABS(CORE_RATING.diff) as ABS_diff, CORE_F.count
FROM (
SELECT post_id_B as post_id, site_rating_A - site_rating_B as diff
FROM POSTS_RATING_DIFFS
WHERE POSTS_RATING_DIFFS.post_id_A = Q_LVL_1.post_id
) as CORE_RATING
LEFT JOIN (
SELECT post_id_winner, post_id_loser, IFNULL(COUNT(*), 0) as count
FROM `verus`
WHERE ip = '{$_SERVER['REMOTE_ADDR']}'
) as CORE_F ON (CORE_RATING.post_id = CORE_F.post_id_winner OR CORE_RATING.post_id = CORE_F.post_id_loser)
WHERE
POSTS_RATING_DIFFS.post_id_A = Q_LVL_1.post_id
AND '<combination of CORE_F.count and CORE_RATING.diff>'
= MAX (
SELECT '<combination of CORE_F_2.count and CORE_RATING_2.diff>'
FROM (
SELECT site_rating_A - site_rating_B as diff
FROM POSTS_RATING_DIFFS
WHERE POSTS_RATING_DIFFS.post_id_A = Q_LVL_1.post_id
) as CORE_RATING_2
LEFT JOIN (
SELECT post_id_winner, post_id_loser, IFNULL(COUNT(*), 0) as count
FROM `verus`
WHERE ip = '{$_SERVER['REMOTE_ADDR']}'
) as CORE_F_2 ON (CORE_RATING_2.post_id = CORE_F_2.post_id_winner OR CORE_RATING_2.post_id = CORE_F_2.post_id_loser)
) /* END MAX */
) AS CORE_P
GROUP BY CORE_P.count, ABS(CORE_P.diff)
) AS CORE_ONE_LINER ON P.post_id = CORE_ONE_LINER.post_id
) AS Q_LVL_2
ORDER BY RAND()
LIMIT 20

limit results for wpdb query

this code displays the tags from current category only but, it gets all the tags (hundreds) so, i need to limit the number of returned results and make em random.
How to make this query get only 20 results randomly ?
/* Retrieve all tags from posts in selected categories */
$cats = array('beaches','mountains'); // Must be an array even if only one category
$cats_string = "'" . implode($cats,"','") . "'";
$sql = <<<EOSQL
SELECT DISTINCT t.*
FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'post_tag')
JOIN $wpdb->terms t ON tt.term_id = t.term_id
WHERE
p.ID IN (
SELECT p2.ID
FROM $wpdb->posts p2
JOIN $wpdb->term_relationships tr2 ON p2.ID = tr2.object_id
JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category')
JOIN $wpdb->terms t2 ON (tt2.term_id = t2.term_id AND t2.name IN ($cats_string))
WHERE p2.post_type = 'post'
AND p2.post_status = 'publish'
AND p2.post_date <= NOW()
)
EOSQL;
$terms = $wpdb->get_results($sql);
// print_r($terms);
echo "<br />";
foreach ($terms as $term) {
echo "ID:$term->term_id NAME:$term->name SLUG:$term->slug<br />";
}
Thanks
You can try an ORDER BY RAND() LIMIT 20, depending on your table size this can run in decent times. See here some details on when to avoid the order by rand() logic. Like suggested, in the specified post, the other approach is to retrieve all the entries and randomly select 20 entries, in PHP rather then using mysql.

SQL WordPress Custom Query Inner Join

I'm having some difficulty with my SQL statement. I'm doing a query on WordPress to display posts based on multiple post meta fields. When I do the query and filter with only one meta field, or a OR on multiple it works, however AND on multiple fails.
SELECT wposts . *
FROM wp_posts wposts
INNER JOIN (
SELECT post_id
FROM wp_postmeta wpostmeta
WHERE (
(wpostmeta.meta_key = 'ulnooweg_business_industry'
AND wpostmeta.meta_value = 'Legal Services')
AND (
wpostmeta.meta_key = 'ulnooweg_business_province'
AND wpostmeta.meta_value = 'New Brunswick')
)
GROUP BY post_id
)
AS t ON t.post_id = wposts.ID
WHERE wposts.post_status = 'publish'
AND wposts.post_type = 'business'
ORDER BY wposts.post_title ASC
LIMIT 0 , 30
Your query is testing if meta_key (and meta_value) is 2 different values in the same row, which is impossible. But I see what you are trying to do..
Try joining the wp_postmeta table twice except each with an ON clause that excludes all rows except those that satisfy the meta_key condition:
SELECT
p.*,
GROUP_CONCAT(CONCAT(pm.meta_key,':',pm.meta_value) SEPARATOR ',') AS meta_values
FROM
wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
JOIN wp_postmeta pm_bi ON (pm_bi.post_id = p.ID AND pm_bi.meta_key = 'ulnooweg_business_industry')
JOIN wp_postmeta pm_bp ON (pm_bp.post_id = p.ID AND pm_bp.meta_key = 'ulnooweg_business_province')
WHERE
pm_bi.meta_value = 'Legal Services'
AND pm_bp.meta_value = 'New Brunswick'
AND p.post_type = 'business'
AND p.post_status = 'publish'
GROUP BY p.ID
ORDER BY p.post_title ASC
Note: I joined the wp_postmeta table 3 times here to help prove that the conditions are satisfied, but you can remove the GROUP_CONCAT line (and the comma on the previous line of course) and the first JOIN to wp_postmeta and the query will work the same.
In the subquery, it looks like it's looking for records where both wpostmeta.meta_key = 'ulnooweg_business_industry' and wpostmeta.meta_key = 'ulnooweg_business_province' -- in other words, wpostmeta.meta_key needs to be equal to two strings simultaneously to satisfy this condition. Also, it's looking for wpostmeta.meta_value = 'Legal Services' and wpostmeta.meta_value = 'New Brunswick'.
My guess is that this is what you want in the WHERE clause of the subquery -- change one of the ANDs to an OR:
....
WHERE (
(wpostmeta.meta_key = 'ulnooweg_business_industry'
AND wpostmeta.meta_value = 'Legal Services')
OR ( -- changed to an OR
wpostmeta.meta_key = 'ulnooweg_business_province'
AND wpostmeta.meta_value = 'New Brunswick')
)
....
The problem is in the where clause of your inner select; I'm guessing wpostmeta returns MULTIPLE rows. A previous comment that a string can't be two values is correct. The 2nd approach should work if the 1st doesn't
at first I thought
WHERE
((wpostmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services') OR
(wpostmeta.meta_key = 'ulnooweg_business_province' AND wpostmeta.meta_value = 'New Brunswick'))
Group by Post_ID
HAVING count(post_ID) = 2
This will work ONLY if there is only one record in the wpostmeta for each type of entry. If
postmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services' can occur twice, then the above does't work.
2nd approach
Select wposts.*
FROM WP_Posts wposts
INNER JOIN (
Select POST_ID from WP_POSTMeta where meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services'
INTERSECT
SELECT POST_ID FROM WP_POST_META WHERE meta_key = 'ulnooweg_business_province' AND wpostmeta.meta_value = 'New Brunswick'
)
AS T on T.Post_ID = wposts.ID