使用带有union的派生表的MySQL列排序问题

时间:2013-07-18 16:09:19

标签: mysql sql union

在此查询中寻找一些帮助。我正在尝试使用p.timer列按降序对结果进行排序。错误是Every derived table must have its own alias所以我认为问题是俱乐部表需要别名,但不确定这是否是问题?

提前感谢您指出我正确的方向。

select *
from (
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 1 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '1,3,4,5,6'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1'
    ORDER BY p.timer DESC LIMIT 0,1)
UNION (
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 2 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '2'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1'
    ORDER BY p.timer DESC LIMIT 0,1)
UNION (
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 3 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '10'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1,2'
    ORDER BY p.timer DESC LIMIT 0,1)
UNION (SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 4 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '11'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1,2'
    ORDER BY p.timer DESC LIMIT 0,1)
UNION (SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 5 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '7'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1'
    ORDER BY p.timer DESC LIMIT 0,1)
UNION (SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 6 as Rank
    FROM `names` as p INNER JOIN `clubs` as g ON p.club_post = '1,2,3,4,5,6'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '2'
    ORDER BY p.timer DESC LIMIT 0,1)
ORDER BY rank,p.timer DESC LIMIT 0, 5

1 个答案:

答案 0 :(得分:2)

您的查询中使用了错误的UNION。这是正确的用法,即使查询很丑陋。

select * from (
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 1 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '1,3,4,5,6'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1'
    ORDER BY p.timer DESC LIMIT 0,1
UNION
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 2 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '2'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1'
    ORDER BY p.timer DESC LIMIT 0,1
UNION
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 3 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '10'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1,2'
    ORDER BY p.timer DESC LIMIT 0,1
UNION
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 4 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '11'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1,2'
    ORDER BY p.timer DESC LIMIT 0,1
UNION
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 5 as Rank
    FROM `names` as p
    INNER JOIN `clubs` as g ON p.club_post = '7'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '1'
    ORDER BY p.timer DESC LIMIT 0,1
UNION
    SELECT p.id,p.Display_Title,p.industry,p.location,p.timer,p.club_post,i.name,l.city,l.StateABBR, 6 as Rank
    FROM `names` as p INNER JOIN `clubs` as g ON p.club_post = '1,2,3,4,5,6'
    INNER JOIN places as l ON l.id = p.location
    INNER JOIN workforce as i ON i.id = p.industry
    WHERE p.status = '1' AND p.status_1 = '1' AND p.category  = '2'
    ORDER BY p.timer DESC LIMIT 0,1
) subtable
ORDER BY rank,p.timer DESC LIMIT 0, 5