2016-07-07 4 views
0

행에 (firstname, lastname, phone, email)이 있습니다. 내가 만들고 싶어하는 경우, 일종의 먼저 firstnamelastname가 null입니다 concat(firstname, lastname) asc 나머지 행에 의해 phone가 null의 종류 인 경우 email에 의해 (concat(firstname, lastname) asc으로 정렬 된 후 올하고 phone/email에 따라 분류해야하는 쿼리입니다 email이 null 일 경우 phone으로 정렬). 어떤 제안이라도 도움이 될 수 있습니까?MySQL 여러 열로 쿼리 순서

+1

'case','coalesce'를 찾으십시오. – potashin

+0

case/coalesce에는 호환 가능한 데이터 유형이 필요합니다. – jarlh

답변

3

이 규칙을 order by에 넣을 수 있습니다.

order by (firstname is null and lastname is null) asc, 
     concat(firstname, lastname), 
     coalesce(phone, email) 

내가 첫 번째 조건은 and 또는 or해야하는지 확실하지 않다 : 나는 이러한 생각한다.

1
select * from table order by concat(firstname, lastname) asc, (firstname is null and lastname is null) asc, coalesce(phone, email)