2014-01-29 94 views
1

我有以下MySQL查詢:有沒有辦法從每個查詢中獲取2行?

SELECT m.id AS id, 'admin' AS type, m.title AS title, m.message AS message, m.link AS link, m.image AS image, 'White' AS colour_scheme 
       FROM x_elder_messages m 
       UNION 
       SELECT a.id AS id, 'evidence' AS type, a.name AS title, a.about AS message, CONCAT('http://www.aaa.com/x/view/ambition/',a.id,'/') AS link, CONCAT('http://www.aaa.com/x/x_images/x_ambitions/current/thumb/',ai.image_id,'.jpg') AS image, 'White' AS colour_scheme 
       FROM x_ambitions a 
       INNER JOIN x_ambition_images ai 
       ON a.id = ai.ambition_id 
       UNION 
       SELECT u.id AS id, 'profile_update' AS type, CONCAT(u.x_first_name,' ',u.x_last_name) AS title, CONCAT(CONCAT(u.x_first_name,' ',u.x_last_name),' has recently updated their profile') AS message, CONCAT('http://www.aaa.com/x/view/',u.id,'/') AS link, CONCAT('http://www.aaa.com/x/x_images/x_users/current/thumb/',i.image_id,'.jpg') AS image, 'Dark' AS colour_scheme 
       FROM x_user u 
       INNER JOIN x_user_images i 
       ON u.id = i.user_id 
       ORDER BY RAND() 

我有輸出有相同的列名,這樣我可以處理結果作爲一個整體。但有沒有辦法限制每個查詢2,所以它會搶2管理員,2個野心,2用戶?

感謝

回答

1

是的,你可以把一個limit在每個子查詢:

(SELECT m.id AS id, 'admin' AS type, m.title AS title, m.message AS message, m.link AS link, m.image AS image, 'White' AS colour_scheme 
       FROM x_elder_messages m 
LIMIT 2 
) UNION 
(SELECT a.id AS id, 'evidence' AS type, a.name AS title, a.about AS message, CONCAT('http://www.aaa.com/x/view/ambition/',a.id,'/') AS link, CONCAT('http://www.aaa.com/x/x_images/x_ambitions/current/thumb/',ai.image_id,'.jpg') AS image, 'White' AS colour_scheme 
       FROM x_ambitions a 
       INNER JOIN x_ambition_images ai 
       ON a.id = ai.ambition_id 
LIMIT 2 
) UNION 
(SELECT u.id AS id, 'profile_update' AS type, CONCAT(u.x_first_name,' ',u.x_last_name) AS title, CONCAT(CONCAT(u.x_first_name,' ',u.x_last_name),' has recently updated their profile') AS message, CONCAT('http://www.aaa.com/x/view/',u.id,'/') AS link, CONCAT('http://www.aaa.com/x/x_images/x_users/current/thumb/',i.image_id,'.jpg') AS image, 'Dark' AS colour_scheme 
       FROM x_user u 
       INNER JOIN x_user_images i 
       ON u.id = i.user_id 
LIMIT 2 
) 
ORDER BY RAND() ; 

此外,你應該使用union all,除非你想故意刪除重複(即增加了額外的處理)。當使用limit時,您通常需要order by指定您獲得的行。

+0

感謝您的快速響應,有沒有什麼辦法,隨機洗牌查詢的整個輸出?我只是簡單地把一個ORDER BY RAND()放在最後? – cwiggo

+0

是的,它的作品,塔! :) – cwiggo

1

試試這個:

SELECT a.id, a.type, a.title, a.message, a.link, a.image, a.colour_scheme 
FROM (SELECT m.id AS id, 'admin' AS TYPE, m.title AS title, m.message AS message, m.link AS link, m.image AS image, 'White' AS colour_scheme 
     FROM x_elder_messages m 
     ORDER BY RAND() LIMIT 2 
    ) AS a 
UNION 
SELECT b.id, b.type, b.title, b.message, b.link, b.image, b.colour_scheme 
FROM (SELECT a.id AS id, 'evidence' AS TYPE, a.name AS title, a.about AS message, CONCAT('http://www.aaa.com/x/view/ambition/',a.id,'/') AS link, CONCAT('http://www.aaa.com/x/x_images/x_ambitions/current/thumb/',ai.image_id,'.jpg') AS image, 'White' AS colour_scheme 
     FROM x_ambitions a 
     INNER JOIN x_ambition_images ai ON a.id = ai.ambition_id 
     ORDER BY RAND() LIMIT 2 
    ) AS b 
UNION 
SELECT c.id, c.type, c.title, c.message, c.link, c.image, c.colour_scheme 
FROM (SELECT u.id AS id, 'profile_update' AS TYPE, CONCAT(u.x_first_name,' ',u.x_last_name) AS title, CONCAT(CONCAT(u.x_first_name,' ',u.x_last_name),' has recently updated their profile') AS message, CONCAT('http://www.aaa.com/x/view/',u.id,'/') AS link, CONCAT('http://www.aaa.com/x/x_images/x_users/current/thumb/',i.image_id,'.jpg') AS image, 'Dark' AS colour_scheme 
     FROM x_user u 
     INNER JOIN x_user_images i ON u.id = i.user_id 
     ORDER BY RAND() LIMIT 2 
    ) AS c 
0

我不熟悉MySQL的SQL,但我建議每個子查詢的限制爲兩行。在Ms-sqlserver上,每個select都會使用'top 2'。爲了能夠區分子集,爲每個子集添加一個靜態值。在第一個select中,您可以在select語句中使用'type = elder_message'。

相關問題