在此查询上寻求一点帮助。我正在尝试使用 p.timer 列按降序对结果进行排序。错误是 Every derived table must have its own alias
所以我认为问题是 clubs 表需要一个别名,但不确定这是否是问题所在?
在此先感谢您为我指明正确的方向。
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
最佳答案
您的查询中使用了错误的 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
关于使用带联合的派生表的 MySQL 列排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17728697/