2012-04-24 204 views
0

我試圖合併這兩個查詢,但我沒有這麼熱與MySQL。我懷疑我需要使用INNER JOIN或子查詢?mysql合併兩個查詢

我試圖合併一個自定義分類與插件。該插件要求我使用SQL訪問我的Wordpress數據庫。下面是從插件查詢:

SELECT p.* 
FROM {$wpdb->prefix}most_popular mp 
INNER JOIN {$wpdb->prefix}posts p ON mp.post_id = p.ID 
WHERE 
    p.post_type = '%s' AND 
    p.post_status = 'publish' 
{$order} 
LIMIT %d 

這是我的分類

SELECT SQL_CALC_FOUND_ROWS wp_posts.* 
FROM wp_posts 
INNER JOIN wp_term_relationships 
ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
    AND (wp_term_relationships.term_taxonomy_id IN (38)) 
    AND wp_posts.post_type = 'post' 
    AND (wp_posts.post_status = 'publish') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date 
DESC LIMIT 0, 10 

我應該如何去融入一個查詢這一切都過濾掉的內容查詢?

+1

一旦你想成爲你的本壘打? – 2012-04-24 16:13:53

+0

@DavidYell我會把它當成收據來閱讀我的問題:P – Jacksonkr 2012-04-24 16:22:20

回答

0
SELECT p.*, wpt.* 
FROM (SELECT * FROM {$wpdb->prefix}terms AS wpt1 WHERE wpt1.name='$loc' LIMIT 1) AS wpt 
    , {$wpdb->prefix}most_popular AS mp 
INNER JOIN {$wpdb->prefix}term_relationships wptr ON (mp.post_id = wptr.object_id) 
INNER JOIN {$wpdb->prefix}posts p ON mp.post_id = p.ID 
WHERE 1=1 
    AND (wptr.term_taxonomy_id = wpt.term_id) 
    AND p.post_type = '%s' 
    AND p.post_status = 'publish' 
{$order} 
LIMIT %d 

它可能是醜陋的,但它的工作原理。