group by 내에서 그룹으로 묶은 것에 대한 정렬하는 방법
조회 2,575 2026-08-18 등록
로그인하면 의견을 남길 수 있습니다.
아직 의견이 없습니다. 첫 의견을 남겨보세요.
게시글
임으로 정렬하기 order by field ( 컬럼명, "순서1", "순서2",......
정렬하는 방법은 여러방법이 있다 그중에 유용한 정렬방법이다 select * from tablename where idx_code in (3,4,1) order by field (idx_code, 1, 4, 3); idx_code 내용을 1,4,3 순으로 정렬하고자 할때 사용하면 편하다
서브쿼리 내용호출할때 사용 댓글수 많은것 순으로 정렬
select *,( select count(*) from table2 where 1 and table1.idx.code=table2.sidx_code //상위에 해당하는내용과 서브에 해당하는 내용을 일치시켜서 갯수호출함 group by sidx_code ) as count //count로 카운트 갯수를 표기함 from table1 where 1 order by count //전체 적인 지리문을 정렬함 첫번째 테이블 table1 두번째 테이블 table2 table1.idx_code값은 table2.sidx_code값과 동일함 sidx_code값은 여러게 존제함
테이블2개일때 차집합,교집합 구하는 방법
A테이블,B테이블 각각존제할때 A.idx_code -> 1,2,3,4 B.idx_code -> 2,3 일때 B.idx_code값을 빼고 A.idx_code -> 1,4만 호출하는 방법 select * from A where code not in (select idx_code from B) -> 차집합을 구할수 있다 select * from A where code in (select idx_code from B) -> 2,3처럼 교집합 내용을 구할수 있다
서브쿼리의 기본
select * from table where 1 and idx_code in (1,2,3,4,5) idx_code값중에 1,2,3,4,5를 가져온다 select * from table where 1 and idx_code not in (1,2,3,4,5) idx_code값중에 1,2,3,4,5아닌것을 가져온다 select * from table where 1 and idx_code in (select sidx_code table1 where 1) 검색된 내역중에 데이타 table1.sidx_code와 table.idx_code가 일치하는 내역만 호출
카테고리가 있는 게시판에서 최근글만 뽑아오는 방법
board 테이블이름 idx_code -> 고유값 b_cate -> 카테고리 b_title -> 게시판명 b_content -> 게시판내용 wdate -> 작성일 하나의 테이블에 카테고리가 여러게 존제하는경우가 있다 이때 각 카테고리별로 최근 글 1개씩만 호출하는 지리문이다 select * from board where 1 and idx_code in (select max(idx_code) from board group by b_cate) order by idx_code desc 서브쿼리를 사용한다 select max(idx_code) from board group by b_cate 카테고리내의 가장큰 값만 호출한다 select * from board where 1 and idx_code in (가장큰값들) order by idx_code desc 그리고 가장큰 값들을 가진 내용만 선택한다
rand()함수로 랜덤하게 정렬하기,select 내용를 랜덤으로 정렬하기
해당 레코드를 순서에 상관없이 랜덤으로 정렬할때는 select * from tablename order by rand() limit 0, 5 이렇게 하면 5개를 랜덤으로 정렬할수 있다 다른 정렬방식으로 최근게시글중 5개를 가저온다은에 그것을 랜던으로 호출하는 방법이다 예를 들어 idx_code -> 1,2,3,4,5,6,7,8,9,10 중에 10,9,8,7,6를 가저온후에 10~6까지를 랜덤으로 불러오는방법이다 (select * from tablename order by idx_code desc limit 0, 5) order by rand()
테이블에서 valign=middle 처럼 div에서 middle 정렬하는 방법
middle를 원하는 div에 display:table-cell;vertical-align:middle; 이렇게 입력하면 테이블의 valign=middle 처럼 정렬될것 같지 만 정렬되지 않는다 감싸고 있는 상위 div에 display:table 을 선언해주었을때 비로소 middle가 적용된다 valign정렬이라는것 자체가 table에 있는 속성이여서 그런지 css내에서도 table-cell 처럼 table단어가 들어가 있다
기간차이를 계산한다음에 정렬하는 방법
날짜별로 정렬하는것은 desc,asc를 통해서 충분히 가능하지만 기간에 대한 차이를 정렬할때는 기간차를 구한후 정렬을 해야한다 이때 계산하는 방법은 (unix_timestamp(edate)-unix_timestamp(sdate)) 방법을 사용하면된다 select *, (unix_timestamp(edate)-unix_timestamp(sdate)) as turm from tablename where 1 order by turm desc
의견 0
로그인하면 의견을 남길 수 있습니다.
아직 의견이 없습니다. 첫 의견을 남겨보세요.