2014-01-10 67 views
3

我已經爲我的包創建了一個查詢。Mysql多重限制

SQL

SELECT SQL_CALC_FOUND_ROWS DISTINCT * FROM wp_posts  
    INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)  
WHERE 1=1 
    AND (((wp_posts.post_title LIKE '% %') 
     OR (wp_posts.post_content LIKE '% %'))) 
    AND wp_posts.post_type = 'listing_type' 
    AND (wp_posts.post_status = 'publish' 
     OR wp_posts.post_status = 'private') 
    AND ((wp_postmeta.meta_key = 'packageID' 
    AND CAST(wp_postmeta.meta_value AS CHAR) = '4' 
     OR CAST(wp_postmeta.meta_value AS CHAR) = '1' 
     OR CAST(wp_postmeta.meta_value AS CHAR) = '2' 
     OR CAST(wp_postmeta.meta_value AS CHAR) = '3' 
     OR CAST(wp_postmeta.meta_value AS CHAR) = '0')) 
GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value ASC, wp_posts.post_date DESC` 

這是工作的罰款。但現在我想在每個meta_key價值中設置限制,因此我可以在我的包中顯示產品。

感謝

SANKET

回答

0

試試這個:

SELECT SQL_CALC_FOUND_ROWS DISTINCT * 
FROM wp_posts wp 
INNER JOIN wp_postmeta wpm ON (wp.ID = wpm.post_id) 
WHERE 1=1 AND (((wp.post_title LIKE '% %') OR (wp.post_content LIKE '% %'))) AND 
     wp.post_type = 'listing_type' AND wp.post_status IN ('publish', 'private') AND 
     wpm.meta_key = 'packageID' AND wpm.meta_value IN (0, 1, 2, 3, 4) 
GROUP BY wp.ID 
ORDER BY wpm.meta_value ASC, wp.post_date DESC 
LIMIT 10 
+0

它工作正常,但我的問題是獲得隨機產品每個meta_value.And的,我需要設置的限制在它。在meta_value ='0'中設置有10個產品,但我想從中顯示兩個隨機記錄。在meta_value ='2'我想從它顯示5個隨機記錄。就像這個我想從SQL獲得。 – sanket