본문 바로가기

카테고리 없음

order by column = 0

이슈 ] 단종된 물품과, 일반 물건이 리스트에 섞임.

 

목표 ] 

랭킹으로 물건 정렬

랭킹 미포함된 물건 정렬

단종된 물건

순으로 나열 

 

 

파이썬 비즈니스로직 특이사항 없음

raw 쿼리 단 분석

 

주요 쿼리문(정렬 part)

ORDER BY tbl.product_rank = 0, tbl.product_rank, tbl.name LIMIT {7}, {8}

order by 에 적힌 내용이 무슨 내용인지 이해가 안갔다. 

구글링을 했다.

 

링크: https://stackoverflow.com/questions/3130197/using-mysql-can-i-sort-a-column-but-have-0-come-last

 

Using MySql, can I sort a column but have 0 come last?

I want to sort by an column of ints ascending, but I want 0 to come last. Is there anyway to do this in MySql?

stackoverflow.com

키워드 : can i sort a column but have 0 come last 

위의 쿼리처럼 수행하면 된다. (이 코드를 작성한 조상님의 의도가 이것이었다: 랭킹을 정렬하되 0으로 된 것은 밑으로 보내버리는 것)

 

위의 쿼리를 사용하면 정렬하되, 0을 포함한 row는 맨 밑으로 보내버린다. 

 

그럼 마찬가지로 is_discontinue 도 단종된 것은 밑으로 보내버리면 되는 것이다. 

 

수정 쿼리

ORDER BY is_discontinue = 1 , tbl.product_rank = 0 , tbl.product_rank asc, tbl.name asc