如何通过ASC订购,但是在mysql的最后排序为null?

时间:2015-11-09 06:37:25

标签: mysql

我在mysql中有以下查询,它按meta_val的升序对行进行排序。但另外我希望将null SELECT rt.tax_id,ra.*, FROM req_tax rt, req_aspects ra, req_meta rm where ra.aspect_id = rt.req_aspects_id and rt.tax_id=rm.req_tax_id and rm.req_id = rt.req_id and rt.requests_id = 18 group by rt.tax_id ORDER BY rm.meta_val asc; 行放在最后。 任何人都可以帮忙吗?

public class ProjectManagement{
WebDriver driver = new FirefoxDriver();

public void navigateCreate(String uid, String pass) throws Throwable {

    driver.manage().window().maximize();
    driver.get(baseurl);
    driver.findElement(By.id("Email")).sendKeys(uid);
    driver.findElement(By.id("Password")).sendKeys(pass);
    driver.findElement(By.id("loginBtn")).click();
    driver.findElement(By.linkText("Projects")).click();
    driver.findElement(By.linkText("Create New Project")).click();
}
}

4 个答案:

答案 0 :(得分:1)

您可以对函数结果进行排序,因此使用if检查该值是否为null,如果是,则返回非常高的值,否则返回该值。理想情况下,您可以根据排序的Max值加1来选择相同类型的值(位数,有符号和无符号),因为这样可以避免类型提升,但这对您来说可能并不重要。如果你想在另一端使用空值,则切换if返回

SELECT rt.tax_id,ra.*, 
FROM req_tax rt, req_aspects ra, req_meta rm 
WHERE ra.aspect_id = rt.req_aspects_id
  AND rt.tax_id=rm.req_tax_id
  AND rm.req_id = rt.req_id
  AND rt.requests_id = 18 
GROUP BY rt.tax_id 
ORDER BY if(rm.meta_val is NULL, 0xFFFFFFFF, rm.meta_val) asc;

答案 1 :(得分:0)

IF子句中使用ORDER BY

ORDER BY 
   IF(rm.meta_val IS NULL, 1, 0),
   rm.meta_val asc;

答案 2 :(得分:0)

尝试使用case

 SELECT rt.tax_id,ra.*,
    FROM req_tax rt, req_aspects ra, req_meta rm where ra.aspect_id = rt.req_aspects_id 
    and rt.tax_id=rm.req_tax_id and rm.req_id = rt.req_id  and rt.requests_id = 18 group by rt.tax_id 
 order by case when rm.meta_val is null then 1 else 0 end, rm.meta_val

答案 3 :(得分:0)

使用ISNULL尝试此查询: -

order by ISNULL(rm.meta_val), rm.meta_val ASC

您的查询: -

SELECT rt.tax_id,ra.*,
FROM req_tax rt, req_aspects ra, req_meta rm where ra.aspect_id = rt.req_aspects_id 
and rt.tax_id=rm.req_tax_id
and rm.req_id = rt.req_id  and rt.requests_id = 18 group by rt.tax_id order by ISNULL(rm.meta_val), rm.meta_val ASC;