你是对的.我无法理解,只要您提供的名称没有以任何方式处理(我无法想到,也许其他人存在),MySQL会接受含糊不清的ORDER BY.
一旦它出现,模棱两可就会被拒绝.
这被接受(并且是多余的):
select b.id, a.name as name
FROM client AS a JOIN client AS b ON (a.id = b.id)
ORDER BY name, name;
COALESCE(名称,”),名称IS NULL,名称OR NULL都被拒绝.
显而易见的解决方案是为别名使用不同的名称,该名称不会出现在任何一个表中.
另一种可能性是创建嵌套查询:
SELECT * FROM ( your query here, without ORDER ) AS original
ORDER BY IF($sortcol is NULL,1,0), $sortcol;
那是:
$sortcol="boat";
$sql = <<
SELECT * FROM (
SELECT fleet,b.boat as boat,owner FROM boats as b
LEFT JOIN owners as o ON b.boat=o.boat
) AS original
ORDER BY IF($sortcol is NULL,1,0), $sortcol;
SQL;