使用带联合的派生表的 MySQL 列排序问题

标签 mysql sql union

在此查询上寻求一点帮助。我正在尝试使用 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/

相关文章:

php - 简单的 PDO 查询返回内存大小错误

MySQL - 根据另一表从一个表中选择

javascript - tomcat App和数据库之间没有连接

c# - 需要合并两个数据集合的帮助

mysql - 使用 Codeigniter 的事件记录模式进行 UNION 查询

php - Category>Sub category>Sub sub category>Sub sub subcategory>产品1

javascript - SQL Server 在一次调用中返回两次结果?

mysql - 连接五个表

java - JDBC:使用 PreparedStatement 创建表,SQL 语法错误与否?

mysql - PHP MySQL,联合表