我有以下MYSQL查询:
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个用户?
由于
答案 0 :(得分: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
来指定您获得的行。
答案 1 :(得分: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
答案 2 :(得分:0)
我不熟悉MySQL SQL,但我建议将每个子查询限制为两行。在Ms-sqlserver上,每个选择都会使用“top 2”。为了能够区分子集,为每个子集添加静态值。在第一个选择中,您可以在select语句'type = elder_message'中使用。