如何对此查询进行排序

时间:2011-08-05 13:56:04

标签: mysql sql

我有mysql查询,我需要按timeaccept命令

    SELECT id, name, level as lvl, leader as par3
FROM Road
WHERE Road.id LIKE '180' 
UNION ALL SELECT id, name, lvl, lastonline, dnd, null, raceid, currenthp, dressed_hp as hp 
FROM Users 
WHERE rid LIKE '180' AND id NOT LIKE (SELECT leader FROM Road WHERE id LIKE '180')
UNION ALL 
SELECT Users.id as id, Users.name, lvl, lastonline, dnd, currentlocid, Location.name as locname, currenthp, dressed_hp as hp 
FROM Users LEFT JOIN Location ON (Location.id = currentlocid) 
WHERE Users.id LIKE(SELECT leader FROM Road WHERE id LIKE '180') ORDER BY Users.timeacceptinvite

2 个答案:

答案 0 :(得分:2)

订单适用于整个工会。您不能指定仅在联合的一部分中使用的Users.timeacceptinvite等别名。您必须指定在联合的最顶部定义的列的名称。

话虽如此,构成你的联盟的各种查询都有不同的列数。这将给出自己的语法错误。作为第一步,确保union的所有部分返回相同数量的列,具有相同的类型。

答案 1 :(得分:1)

select x.* from (
  SELECT
    id, name, level as lvl, leader as par3, timeacceptinvite
  FROM Road
  WHERE Road.id LIKE '180' 
  UNION ALL
  SELECT
    id, name, lvl, lastonline, dnd, null, raceid, currenthp, dressed_hp as hp, timeacceptinvite 
  FROM Users 
  WHERE rid LIKE '180'
    AND id NOT LIKE (SELECT leader FROM Road WHERE id LIKE '180')
  UNION ALL 
  SELECT
   Users.id as id, Users.name, lvl, lastonline, dnd, currentlocid, Location.name as locname, currenthp, dressed_hp as hp, timeacceptinvite 
  FROM Users LEFT JOIN Location ON (Location.id = currentlocid) 
  WHERE Users.id LIKE(SELECT leader FROM Road WHERE id LIKE '180')
) as x
 ORDER BY x.timeacceptinvite

编辑1

但我认为你会收到不同数量的列错误...