1

寻找有关此查询的一些帮助。我正在尝试使用 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
4

1 回答 1

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
于 2013-07-18T18:16:26.587 回答